JAVA客户端调用memcached

 

网址:http://www.whalin.com/memcached
最新版本:java_memcached-release_2.0.1

操作示例:
Java代码
  1. importcom.danga.MemCached.*;
  2. importorg.apache.log4j.*;
  3. publicclassTestMemcached{
  4. publicstaticvoidmain(String[]args){
  5. /*初始化SockIOPool,管理memcached的连接池*/
  6. String[]servers={"192.168.1.20:12111"};
  7. SockIOPoolpool=SockIOPool.getInstance();
  8. pool.setServers(servers);
  9. pool.setFailover(true);
  10. pool.setInitConn(10);
  11. pool.setMinConn(5);
  12. pool.setMaxConn(250);
  13. pool.setMaintSleep(30);
  14. pool.setNagle(false);
  15. pool.setSocketTO(3000);
  16. pool.setAliveCheck(true);
  17. pool.initialize();
  18. /*建立MemcachedClient实例*/
  19. MemCachedClientmemCachedClient=newMemCachedClient();
  20. for(inti=0;i<10;i++){
  21. /*将对象加入到memcached缓存*/
  22. booleansuccess=memCachedClient.set(""+i,"Hello!");
  23. /*从memcached缓存中按key值取对象*/
  24. Stringresult=(String)memCachedClient.get(""+i);
  25. System.out.println(String.format("set(%d):%s",i,success));
  26. System.out.println(String.format("get(%d):%s",i,result));
  27. }
  28. }
  29. }
Java代码
  1. importcom.danga.MemCached.*;
  2. importorg.apache.log4j.*;
  3. publicclassTestMemcached{
  4. publicstaticvoidmain(String[]args){
  5. /*初始化SockIOPool,管理memcached的连接池*/
  6. String[]servers={"192.168.1.20:12111"};
  7. SockIOPoolpool=SockIOPool.getInstance();
  8. pool.setServers(servers);
  9. pool.setFailover(true);
  10. pool.setInitConn(10);
  11. pool.setMinConn(5);
  12. pool.setMaxConn(250);
  13. pool.setMaintSleep(30);
  14. pool.setNagle(false);
  15. pool.setSocketTO(3000);
  16. pool.setAliveCheck(true);
  17. pool.initialize();
  18. /*建立MemcachedClient实例*/
  19. MemCachedClientmemCachedClient=newMemCachedClient();
  20. for(inti=0;i<10;i++){
  21. /*将对象加入到memcached缓存*/
  22. booleansuccess=memCachedClient.set(""+i,"Hello!");
  23. /*从memcached缓存中按key值取对象*/
  24. Stringresult=(String)memCachedClient.get(""+i);
  25. System.out.println(String.format("set(%d):%s",i,success));
  26. System.out.println(String.format("get(%d):%s",i,result));
  27. }
  28. }
  29. }


2.spymemcached客户端API:spymemcached client
网址:http://code.google.com/p/spymemcached/
最新版本:memcached-2.1.jar

操作示例:
用spymemcached将对象存入缓存
Java代码
  1. importjava.net.InetSocketAddress;
  2. importjava.util.concurrent.Future;
  3. importnet.spy.memcached.MemcachedClient;
  4. publicclassMClient{
  5. publicstaticvoidmain(String[]args){
  6. try{
  7. /*建立MemcachedClient实例,并指定memcached服务的IP地址和端口号*/
  8. MemcachedClientmc=newMemcachedClient(newInetSocketAddress("192.168.1.20",12111));
  9. Future<Boolean>b=null;
  10. /*将key值,过期时间(秒)和要缓存的对象set到memcached中*/
  11. b=mc.set("neea:testDaF:ksIdno",900,"someObject");
  12. if(b.get().booleanValue()==true){
  13. mc.shutdown();
  14. }
  15. }
  16. catch(Exceptionex){
  17. ex.printStackTrace();
  18. }
  19. }
  20. }
Java代码
  1. importjava.net.InetSocketAddress;
  2. importjava.util.concurrent.Future;
  3. importnet.spy.memcached.MemcachedClient;
  4. publicclassMClient{
  5. publicstaticvoidmain(String[]args){
  6. try{
  7. /*建立MemcachedClient实例,并指定memcached服务的IP地址和端口号*/
  8. MemcachedClientmc=newMemcachedClient(newInetSocketAddress("192.168.1.20",12111));
  9. Future<Boolean>b=null;
  10. /*将key值,过期时间(秒)和要缓存的对象set到memcached中*/
  11. b=mc.set("neea:testDaF:ksIdno",900,"someObject");
  12. if(b.get().booleanValue()==true){
  13. mc.shutdown();
  14. }
  15. }
  16. catch(Exceptionex){
  17. ex.printStackTrace();
  18. }
  19. }
  20. }


用spymemcached从缓存中取得对象
Java代码
  1. importjava.net.InetSocketAddress;
  2. importjava.util.concurrent.Future;
  3. importnet.spy.memcached.MemcachedClient;
  4. publicclassMClient{
  5. publicstaticvoidmain(String[]args){
  6. try{
  7. /*建立MemcachedClient实例,并指定memcached服务的IP地址和端口号*/
  8. MemcachedClientmc=newMemcachedClient(newInetSocketAddress("192.168.1.20",12111));
  9. /*按照key值从memcached中查找缓存,不存在则返回null*/
  10. Objectb=mc.get("neea:testDaF:ksIdno");
  11. mc.shutdown();
  12. }
  13. catch(Exceptionex){
  14. ex.printStackTrace();
  15. }
  16. }
  17. }
Java代码
  1. importjava.net.InetSocketAddress;
  2. importjava.util.concurrent.Future;
  3. importnet.spy.memcached.MemcachedClient;
  4. publicclassMClient{
  5. publicstaticvoidmain(String[]args){
  6. try{
  7. /*建立MemcachedClient实例,并指定memcached服务的IP地址和端口号*/
  8. MemcachedClientmc=newMemcachedClient(newInetSocketAddress("192.168.1.20",12111));
  9. /*按照key值从memcached中查找缓存,不存在则返回null*/
  10. Objectb=mc.get("neea:testDaF:ksIdno");
  11. mc.shutdown();
  12. }
  13. catch(Exceptionex){
  14. ex.printStackTrace();
  15. }
  16. }
  17. }
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
使用例子
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>

<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>
</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", "你好!");
//设置带有过期时间的例子
//过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();}
//jsp 使用请参考test.jsp文件
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。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值