在当前的一个语音系统中,需要频繁进行I/O操作, 为了应付未来可能出现的高并发访问, 初步计划引入缓存机制。
在诸多缓存中, ehcache口碑不错, 我之前也写过一篇文章介绍ehcache的使用:
http://lcllcl987.iteye.com/blog/222693
但ehcache不是一个分布式缓存解决方案。
所以, 就初选memcached了:
http://www.danga.com/memcached/
memcached的安装交给google,略过不表。
下面是两个memcached client的java实现:
http://www.whalin.com/memcached/
http://code.google.com/p/spymemcached/
以
http://www.whalin.com/memcached/
为例, 它的文档不错, 就不多说了。 test如下:
在诸多缓存中, ehcache口碑不错, 我之前也写过一篇文章介绍ehcache的使用:
http://lcllcl987.iteye.com/blog/222693
但ehcache不是一个分布式缓存解决方案。
所以, 就初选memcached了:
http://www.danga.com/memcached/
memcached的安装交给google,略过不表。
下面是两个memcached client的java实现:
http://www.whalin.com/memcached/
http://code.google.com/p/spymemcached/
以
http://www.whalin.com/memcached/
为例, 它的文档不错, 就不多说了。 test如下:
- packageco.cache;
- importcom.danga.MemCached.MemCachedClient;
- importcom.danga.MemCached.SockIOPool;
- publicclassMyMemCachedClient
- {
- //createastaticclientasmostinstallsonlyneed
- //asingleinstance
- privatestaticMemCachedClientmemCachedClient=newMemCachedClient();
- //setupconnectionpoolonceatclassload
- static
- {
- //serverlistandweights
- String[]servers={"192.168.0.55:11211"};
- Integer[]weights={3};
- //grabaninstanceofourconnectionpool
- SockIOPoolpool=SockIOPool.getInstance();
- //settheserversandtheweights
- pool.setServers(servers);
- pool.setWeights(weights);
- //setsomebasicpoolsettings
- //5initial,5min,and250maxconns
- //andsetthemaxidletimeforaconn
- //to6hours
- pool.setInitConn(5);
- pool.setMinConn(5);
- pool.setMaxConn(250);
- pool.setMaxIdle(1000*60*60*6);
- //setthesleepforthemaintthread
- //itwillwakeupeveryxsecondsand
- //maintainthepoolsize
- pool.setMaintSleep(30);
- //setsomeTCPsettings
- //disablenagle
- //setthereadtimeoutto3secs
- //anddon'tsetaconnecttimeout
- pool.setNagle(false);
- pool.setSocketTO(3000);
- pool.setSocketConnectTO(0);
- //initializetheconnectionpool
- pool.initialize();
- //letssetsomecompressiononfortheclient
- //compressanythinglargerthan64k
- memCachedClient.setCompressEnable(true);
- memCachedClient.setCompressThreshold(64*1024);
- }
- publicstaticMemCachedClientgetMemCachedClient()
- {
- returnmemCachedClient;
- }
- //fromhereondown,youcancallanyoftheclientcalls
- publicstaticvoidexamples()
- {
- memCachedClient.set("foo","ThisisatestString");
- Stringbar=(String)memCachedClient.get("foo");
- }
- publicstaticvoidmain(String[]args)
- {
- MyMemCachedClient.getMemCachedClient().set("foo","ThisisatestString.");
- Stringbar=(String)MyMemCachedClient.getMemCachedClient().get("foo");
- System.out.println(bar);
- }
- }
- 和ehcache不同的是,memcached似乎只采用LRU算法, 还对付cache满员的情况。
本文探讨了在语音系统中引入缓存机制的重要性,并对比了ehcache与Memcached的特点。最终选择Memcached作为缓存解决方案,并提供了Java客户端的配置及使用示例。
498

被折叠的 条评论
为什么被折叠?



