网址:http://www.whalin.com/memcached
最新版本:java_memcached-release_2.0.1
操作示例:
最新版本: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:spymemcached client
网址: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();
- }
- }
- }
用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));
- /*按照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();
- }
- }
- }
3.alisoft-xplatform-asf-cache-2.4
这是国产的memcached client.我花时间测试过,配置简单,使用也简单,有很详细的中文文档和英文文档.
网址: http://code.google.com/p/memcache-client-forjava/
最新版本: alisoft-xplatform-asf-cache-2.5.2.jar
网址: http://code.google.com/p/memcache-client-forjava/
最新版本: alisoft-xplatform-asf-cache-2.5.2.jar
使用例子
memcached.xml
=========================
<?xml version="1.0" encoding="UTF-8"?>
<memcached>
<client name="mclient0" compressEnable="true" defaultEncoding="UTF-8" socketpool="pool0">
<errorHandler>com.moit.xplatform.asf.cache.memcached.MemcachedErrorHandler</errorHandler>
</client>
<client name="mclient1" compressEnable="true" defaultEncoding="UTF-8" socketpool="pool1">
<errorHandler>com.moit.xplatform.asf.cache.memcached.MemcachedErrorHandler</errorHandler>
</client>
<client name="mclient2" compressEnable="true" defaultEncoding="UTF-8" socketpool="pool2">
<errorHandler>com.moit.xplatform.asf.cache.memcached.MemcachedErrorHandler</errorHandler>
</client>
<memcached>
<client name="mclient0" compressEnable="true" defaultEncoding="UTF-8" socketpool="pool0">
<errorHandler>com.moit.xplatform.asf.cache.memcached.MemcachedErrorHandler</errorHandler>
</client>
<client name="mclient1" compressEnable="true" defaultEncoding="UTF-8" socketpool="pool1">
<errorHandler>com.moit.xplatform.asf.cache.memcached.MemcachedErrorHandler</errorHandler>
</client>
<client name="mclient2" compressEnable="true" defaultEncoding="UTF-8" socketpool="pool2">
<errorHandler>com.moit.xplatform.asf.cache.memcached.MemcachedErrorHandler</errorHandler>
</client>
<socketpool name="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>
<socketpool name="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>
<socketpool name="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>
<cluster name="cluster1">
<memCachedClients>mclient1,mclient2</memCachedClients>
</cluster>
<memCachedClients>mclient1,mclient2</memCachedClients>
</cluster>
</memcached>
//用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服务器,没有数据,则去取数据库数据!");
}
例子:
static ICacheManager<IMemcachedCache> manager;
/**
* 测试MemCached
* @return
*/
public String memcache()
{
manager = CacheUtil.getCacheManager(IMemcachedCache.class,
MemcachedCacheManager.class.getName());
manager.start();
try
{
IMemcachedCache cache = manager.getCache("mclient0");
//根据key得到缓存数据
String a =(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", "你好!");
//设置带有过期时间的例子
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服务器,没有数据,则去取数据库数据!");
}
例子:
static ICacheManager<IMemcachedCache> manager;
/**
* 测试MemCached
* @return
*/
public String memcache()
{
manager = CacheUtil.getCacheManager(IMemcachedCache.class,
MemcachedCacheManager.class.getName());
manager.start();
try
{
IMemcachedCache cache = manager.getCache("mclient0");
//根据key得到缓存数据
String a =(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", "你好!");
//设置带有过期时间的例子
//过30分钟
Calendar calendar = Calendar.getInstance();//当前日期
calendar.setTime(new Date());
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();}
Calendar calendar = Calendar.getInstance();//当前日期
calendar.setTime(new Date());
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();}
//jsp 使用请参考test.jsp文件
return "testmempage";
}
return "testmempage";
}
总结:三种API比较
memcached client for java:较早推出的memcached JAVA客户端API,应用广泛,运行比较稳定。
spymemcached:A simple, asynchronous, single-threaded memcached client written in java. 支持异步,单线程的memcached客户端,用到了java1.5版本的concurrent和nio,存取速度会高于前者,但是稳定性不好,测试中常报timeOut等相关异常。
alisoft-xplatform-asf-cache-2.4 我开始采用,后来发现性能很差,尤其是调用频繁时特别容易出问题.我后来还是采用第一个.现在还没发现什么问题.
由于memcached client for java发布了新版本,性能上有所提高,并且运行稳定,所以建议使用memcached client for java。
由于memcached client for java发布了新版本,性能上有所提高,并且运行稳定,所以建议使用memcached client for java。