memCache

Memcache的介绍有很多,这里给出如何在Java中应用Memcache的基本方法

1安装Memcache服务器(windows)

下载windows版Memcache安装包,如memcached-1.2.6-win32-bin.zip,解压到指定位置,比如(D://memcache),打开dos命令行,输入以下两个命令即可启动Memcache服务。

D:/memcache/memcached.exe-dinstall

D:/memcache/memcached.exe-dstart

2下载Java版本的memcache客户端(以下列出常用的几种)。

spymemcached:

http://code.google.com/p/spymemcached/

gwhalin/Memcached-Java-Client

https://github.com/gwhalin/Memcached-Java-Client/downloads

Jcache

http://code.google.com/intl/zh-CN/appengine/docs/java/memcache/usingjcache.html

3下面给出两种方式调用Memcache

gwhalin/Memcached-Java-Client调用方式如下:

[java:nogutter]viewplaincopyprint?publicclassMemcacheManagerForGwhalin{

//构建缓存客户端

privatestaticMemCachedClientcachedClient;

//单例模式实现客户端管理类

privatestaticMemcacheManagerForGwhalinINSTANCE=newMemcacheManagerForGwhalin();

privateMemcacheManagerForGwhalin(){

cachedClient=newMemCachedClient();

//获取连接池实例

SockIOPoolpool=SockIOPool.getInstance();

//设置缓存服务器地址,可以设置多个实现分布式缓存

pool.setServers(newString[]{"127.0.0.1:11211"});

//设置初始连接5

pool.setInitConn(5);

//设置最小连接5

pool.setMinConn(5);

//设置最大连接250

pool.setMaxConn(250);

//设置每个连接最大空闲时间3个小时

pool.setMaxIdle(1000*60*60*3);

pool.setMaintSleep(30);

pool.setNagle(false);

pool.setSocketTO(3000);

pool.setSocketConnectTO(0);

pool.initialize();

}

/**

*获取缓存管理器唯一实例

*@return

*/

publicstaticMemcacheManagerForGwhalingetInstance(){

returnINSTANCE;

}

@Override

publicvoidadd(Stringkey,Objectvalue){

cachedClient.set(key,value);

}

@Override

publicvoidadd(Stringkey,Objectvalue,intmilliseconds){

cachedClient.set(key,value,milliseconds);

}

@Override

publicvoidremove(Stringkey){

cachedClient.delete(key);

}

@Override

publicvoidremove(Stringkey,intmilliseconds){

cachedClient.delete(key,milliseconds,newDate());

}

@Override

publicvoidupdate(Stringkey,Objectvalue,intmilliseconds){

cachedClient.replace(key,value,milliseconds);

}

@Override

publicvoidupdate(Stringkey,Objectvalue){

cachedClient.replace(key,value);

}

@Override

publicObjectget(Stringkey){

returncachedClient.get(key);

}

}

publicclassMemcacheManagerForGwhalin{

//构建缓存客户端

privatestaticMemCachedClientcachedClient;

//单例模式实现客户端管理类

privatestaticMemcacheManagerForGwhalinINSTANCE=newMemcacheManagerForGwhalin();

privateMemcacheManagerForGwhalin(){

cachedClient=newMemCachedClient();

//获取连接池实例

SockIOPoolpool=SockIOPool.getInstance();

//设置缓存服务器地址,可以设置多个实现分布式缓存

pool.setServers(newString[]{"127.0.0.1:11211"});

//设置初始连接5

pool.setInitConn(5);

//设置最小连接5

pool.setMinConn(5);

//设置最大连接250

pool.setMaxConn(250);

//设置每个连接最大空闲时间3个小时

pool.setMaxIdle(1000*60*60*3);

pool.setMaintSleep(30);

pool.setNagle(false);

pool.setSocketTO(3000);

pool.setSocketConnectTO(0);

pool.initialize();

}

/**

*获取缓存管理器唯一实例

*@return

*/

publicstaticMemcacheManagerForGwhalingetInstance(){

returnINSTANCE;

}

@Override

publicvoidadd(Stringkey,Objectvalue){

cachedClient.set(key,value);

}

@Override

publicvoidadd(Stringkey,Objectvalue,intmilliseconds){

cachedClient.set(key,value,milliseconds);

}

@Override

publicvoidremove(Stringkey){

cachedClient.delete(key);

}

@Override

publicvoidremove(Stringkey,intmilliseconds){

cachedClient.delete(key,milliseconds,newDate());

}

@Override

publicvoidupdate(Stringkey,Objectvalue,intmilliseconds){

cachedClient.replace(key,value,milliseconds);

}

@Override

publicvoidupdate(Stringkey,Objectvalue){

cachedClient.replace(key,value);

}

@Override

publicObjectget(Stringkey){

returncachedClient.get(key);

}

}

Spy方式调用如下:

publicclassMemcacheManagerForSpyimplementsIMemcacheManager{

//缓存客户端

privateMemcachedClientmemcacheCient;

//Manager管理对象,单例模式

privatestaticMemcacheManagerForSpyINSTANCE=newMemcacheManagerForSpy();

privateMemcacheManagerForSpy(){

try{

memcacheCient=newMemcachedClient(newInetSocketAddress("127.0.0.1",11211));

}catch(IOExceptione){

e.printStackTrace();

}

}

publicstaticMemcacheManagerForSpygetInstance(){

returnINSTANCE;

}

@Override

publicvoidadd(Stringkey,Objectvalue,intmilliseconds){

memcacheCient.add(key,milliseconds,value);

}

@Override

publicvoidadd(Stringkey,Objectvalue){

memcacheCient.add(key,3600,value);

}

@Override

publicvoidremove(Stringkey,intmilliseconds){

memcacheCient.delete(key);

}

@Override

publicvoidremove(Stringkey){

memcacheCient.delete(key);

}

@Override

publicvoidupdate(Stringkey,Objectvalue,intmilliseconds){

memcacheCient.replace(key,milliseconds,value);

}

@Override

publicvoidupdate(Stringkey,Objectvalue){

memcacheCient.replace(key,3600,value);

}

@Override

publicObjectget(Stringkey){

returnmemcacheCient.get(key);

}

}

相关推荐