JAVA客户端调用memcached
最新版本:java_memcached-release_2.0.1
操作示例:importcom.danga.MemCached.*; importorg.apache.log4j.*; publicclassTestMemcached{ publicstaticvoidmain(String[]args){ /*初始化SockIOPool,管理memcached的连接池*/ String[]servers={"192.168.1.20:12111"}; SockIOPoolpool=SockIOPool.getInstance(); pool.setServers(servers); pool.setFailover(true); pool.setInitConn(10); pool.setMinConn(5); pool.setMaxConn(250); pool.setMaintSleep(30); pool.setNagle(false); pool.setSocketTO(3000); pool.setAliveCheck(true); pool.initialize(); /*建立MemcachedClient实例*/ MemCachedClientmemCachedClient=newMemCachedClient(); for(inti=0;i<10;i++){ /*将对象加入到memcached缓存*/ booleansuccess=memCachedClient.set(""+i,"Hello!"); /*从memcached缓存中按key值取对象*/ Stringresult=(String)memCachedClient.get(""+i); System.out.println(String.format("set(%d):%s",i,success)); System.out.println(String.format("get(%d):%s",i,result)); } } }
importcom.danga.MemCached.*; importorg.apache.log4j.*; publicclassTestMemcached{ publicstaticvoidmain(String[]args){ /*初始化SockIOPool,管理memcached的连接池*/ String[]servers={"192.168.1.20:12111"}; SockIOPoolpool=SockIOPool.getInstance(); pool.setServers(servers); pool.setFailover(true); pool.setInitConn(10); pool.setMinConn(5); pool.setMaxConn(250); pool.setMaintSleep(30); pool.setNagle(false); pool.setSocketTO(3000); pool.setAliveCheck(true); pool.initialize(); /*建立MemcachedClient实例*/ MemCachedClientmemCachedClient=newMemCachedClient(); for(inti=0;i<10;i++){ /*将对象加入到memcached缓存*/ booleansuccess=memCachedClient.set(""+i,"Hello!"); /*从memcached缓存中按key值取对象*/ Stringresult=(String)memCachedClient.get(""+i); System.out.println(String.format("set(%d):%s",i,success)); System.out.println(String.format("get(%d):%s",i,result)); } } }
2.spymemcached客户端API:spymemcachedclient
网址:http://code.google.com/p/spymemcached/
最新版本:memcached-2.1.jar
操作示例:
用spymemcached将对象存入缓存importjava.net.InetSocketAddress; importjava.util.concurrent.Future; importnet.spy.memcached.MemcachedClient; publicclassMClient{ publicstaticvoidmain(String[]args){ try{ /*建立MemcachedClient实例,并指定memcached服务的IP地址和端口号*/ MemcachedClientmc=newMemcachedClient(newInetSocketAddress("192.168.1.20",12111)); Future<Boolean>b=null; /*将key值,过期时间(秒)和要缓存的对象set到memcached中*/ b=mc.set("neea:testDaF:ksIdno",900,"someObject"); if(b.get().booleanValue()==true){ mc.shutdown(); } } catch(Exceptionex){ ex.printStackTrace(); } } }
importjava.net.InetSocketAddress; importjava.util.concurrent.Future; importnet.spy.memcached.MemcachedClient; publicclassMClient{ publicstaticvoidmain(String[]args){ try{ /*建立MemcachedClient实例,并指定memcached服务的IP地址和端口号*/ MemcachedClientmc=newMemcachedClient(newInetSocketAddress("192.168.1.20",12111)); Future<Boolean>b=null; /*将key值,过期时间(秒)和要缓存的对象set到memcached中*/ b=mc.set("neea:testDaF:ksIdno",900,"someObject"); if(b.get().booleanValue()==true){ mc.shutdown(); } } catch(Exceptionex){ ex.printStackTrace(); } } }
importjava.net.InetSocketAddress; importjava.util.concurrent.Future; importnet.spy.memcached.MemcachedClient; publicclassMClient{ publicstaticvoidmain(String[]args){ try{ /*建立MemcachedClient实例,并指定memcached服务的IP地址和端口号*/ MemcachedClientmc=newMemcachedClient(newInetSocketAddress("192.168.1.20",12111)); /*按照key值从memcached中查找缓存,不存在则返回null*/ Objectb=mc.get("neea:testDaF:ksIdno"); mc.shutdown(); } catch(Exceptionex){ ex.printStackTrace(); } } }
importjava.net.InetSocketAddress; importjava.util.concurrent.Future; importnet.spy.memcached.MemcachedClient; publicclassMClient{ publicstaticvoidmain(String[]args){ try{ /*建立MemcachedClient实例,并指定memcached服务的IP地址和端口号*/ MemcachedClientmc=newMemcachedClient(newInetSocketAddress("192.168.1.20",12111)); /*按照key值从memcached中查找缓存,不存在则返回null*/ Objectb=mc.get("neea:testDaF:ksIdno"); mc.shutdown(); } catch(Exceptionex){ ex.printStackTrace(); } } }
网址:http://code.google.com/p/memcache-client-forjava/
最新版本: alisoft-xplatform-asf-cache-2.5.2.jar<memcached>
<clientname="mclient0"compressEnable="true"defaultEncoding="UTF-8"socketpool="pool0">
<errorHandler>com.moit.xplatform.asf.cache.memcached.MemcachedErrorHandler</errorHandler>
</client>
<clientname="mclient1"compressEnable="true"defaultEncoding="UTF-8"socketpool="pool1">
<errorHandler>com.moit.xplatform.asf.cache.memcached.MemcachedErrorHandler</errorHandler>
</client>
<clientname="mclient2"compressEnable="true"defaultEncoding="UTF-8"socketpool="pool2">
<errorHandler>com.moit.xplatform.asf.cache.memcached.MemcachedErrorHandler</errorHandler>
</client><socketpoolname="pool0"failover="true"initConn="5"minConn="5"maxConn="250"maintSleep="0"
nagle="false"socketTO="3000"aliveCheck="true">
<servers>192.168.3.251:11211,192.168.3.251:11211</servers>
</socketpool>
<socketpoolname="pool1"failover="true"initConn="5"minConn="5"maxConn="250"maintSleep="0"
nagle="false"socketTO="3000"aliveCheck="true">
<servers>192.168.3.251:11211,192.168.3.251:11211</servers>
</socketpool>
<socketpoolname="pool2"failover="true"initConn="5"minConn="5"maxConn="250"maintSleep="0"
nagle="false"socketTO="3000"aliveCheck="true">
<servers>192.168.3.251:11211,192.168.3.251:11211</servers>
</socketpool><memCachedClients>mclient1,mclient2</memCachedClients>
</cluster>System.out.println("是否包含了key的数据="+cache.containsKey("key"));
if(cache.containsKey("key"))
{
System.out.println("包含了key的数据");
System.out.println("从cache服务器获得key值");
}
else
{
System.out.println("没有包含了key的数据");
System.out.println("cache服务器,没有数据,则去取数据库数据!");
}
例子:
staticICacheManager<IMemcachedCache>manager;
/**
*测试MemCached
*@return
*/
publicStringmemcache()
{
manager=CacheUtil.getCacheManager(IMemcachedCache.class,
MemcachedCacheManager.class.getName());
manager.start();
try
{
IMemcachedCachecache=manager.getCache("mclient0");
//根据key得到缓存数据
Stringa=(String)cache.get("key");
//用containsKey方法判断cache服务器按指定的key值是否存在。
System.out.println("是否包含了key的数据="+cache.containsKey("key"));
if(cache.containsKey("key"))
{
System.out.println("包含了key的数据");
System.out.println("从cache服务器获得key值");
}
else
{
System.out.println("没有包含了key的数据");
System.out.println("cache服务器,没有数据,则去取数据库数据!");
}
//根据key删除服务器上的对应的缓存数据
cache.remove("key");
//根据key保存数据到服务器上
cache.put("key","你好!");
//设置带有过期时间的例子Calendarcalendar=Calendar.getInstance();//当前日期
calendar.setTime(newDate());
calendar.add(Calendar.MINUTE,30);//
cache.remove("keytime");
cache.put("keytime","30分钟后过期",calendar.getTime());
System.out.println("30分钟后过期=keytime="+cache.get("keytime"));
System.out.println("cache服务器getTime="+calendar.getTime());
}finally{ manager.stop();}return"testmempage";
}总结:三种API比较
memcachedclientforjava:较早推出的memcachedJAVA客户端API,应用广泛,运行比较稳定。
spymemcached:A simple, asynchronous, single-threaded memcached client written in java. 支持异步,单线程的memcached客户端,用到了java1.5版本的concurrent和nio,存取速度会高于前者,但是稳定性不好,测试中常报timeOut等相关异常。