应为是第一次写memcache的文章,将java的驱动包附于附件中。
初始化:memcache
创建一个client对象:
创建一个缓存:
通过key删除一个缓存:
通过key获取缓存对象:
获取多个缓存对象:
刷新全部缓存:
初始化:memcache
static {
String[] serverlist = { "server1.com:port", "server2.com:port" };
SockIOPool pool = SockIOPool.getInstance();
pool.setServers(serverlist);
pool.initialize();
}
创建一个client对象:
MemCachedClient mc = new MemCachedClient();
创建一个缓存:
MemCachedClient mc = new MemCachedClient();
String key = "cacheKey1";
Object value = SomeClass.getObject();
mc.set(key, value);
通过key删除一个缓存:
MemCachedClient mc = new MemCachedClient();
String key = "cacheKey1";
mc.delete(key);
通过key获取缓存对象:
MemCachedClient mc = new MemCachedClient();
String key = "key";
Object value = mc.get(key);
获取多个缓存对象:
MemCachedClient mc = new MemCachedClient();
String[] keys = { "key", "key1", "key2" };
Map<Object> values = mc.getMulti(keys);
刷新全部缓存:
MemCachedClient mc = new MemCachedClient();
mc.flushAll();