在当前的一个语音系统中,需要频繁进行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如下:
package co.cache;
import com.danga.MemCached.MemCachedClient;
import com.danga.MemCached.SockIOPool;
public class MyMemCachedClient
{
// create a static client as most installs only need
// a single instance
private static MemCachedClient memCachedClient = new MemCachedClient();
// set up connection pool once at class load
static
{
// server list and weights
String[] servers = {"192.168.0.55:11211"};
Integer[] weights = {3};
// grab an instance of our connection pool
SockIOPool pool = SockIOPool.getInstance();
// set the servers and the weights
pool.setServers(servers);
pool.setWeights(weights);
// set some basic pool settings
// 5 initial, 5 min, and 250 max conns
// and set the max idle time for a conn
// to 6 hours
pool.setInitConn(5);
pool.setMinConn(5);
pool.setMaxConn(250);
pool.setMaxIdle(1000 * 60 * 60 * 6);
// set the sleep for the maint thread
// it will wake up every x seconds and
// maintain the pool size
pool.setMaintSleep(30);
// set some TCP settings
// disable nagle
// set the read timeout to 3 secs
// and don't set a connect timeout
pool.setNagle(false);
pool.setSocketTO(3000);
pool.setSocketConnectTO(0);
// initialize the connection pool
pool.initialize();
// lets set some compression on for the client
// compress anything larger than 64k
memCachedClient.setCompressEnable(true);
memCachedClient.setCompressThreshold(64 * 1024);
}
public static MemCachedClient getMemCachedClient()
{
return memCachedClient;
}
// from here on down, you can call any of the client calls
public static void examples()
{
memCachedClient.set("foo", "This is a test String");
String bar = (String) memCachedClient.get("foo");
}
public static void main(String[] args)
{
MyMemCachedClient.getMemCachedClient().set("foo", "This is a test String.");
String bar = (String)MyMemCachedClient.getMemCachedClient().get("foo");
System.out.println(bar);
}
}
ehcache有溢出策略, 就是分配的内存满了, 可以持久化到硬盘。
找了一下memcached在这方面的处理, 居然没发现这方面的东东, 哪位兄台能否提供一下。