Memcached提升web数据操作读写性能
package com.boonya.mecache;import com.danga.MemCached.MemCachedClient;import com.danga.MemCached.SockIOPool;public class TestMemCached { /** * @param args */ public static void main(String[] args) { /** * 初始化SockIoPool,管理MemCached线程池 */ String servers[]={"192.168.20.37:11211"}; SockIOPool pool=SockIOPool.getInstance(); pool.setServers(servers); pool.setFailover(true); pool.setInitConn(10); pool.setMinConn(5); pool.setMaxConn(300); pool.setMaintSleep(30); pool.setNagle(false); pool.setSocketTO(3000); pool.setAliveCheck(true); pool.initialize(); /** * 创建MemCachedClient实例 */ MemCachedClient memcacheClient=new MemCachedClient(); for (int i = 0; i < 100; i++) { boolean success=memcacheClient.set("key"+i,"a_memcached");//将对象加入到MemCache缓存 /** * 读取MemCache缓存 */ String result=(String) memcacheClient.get("key"+i); System.out.println(String.format("set{%d}:%s",i,success)); System.out.println(String.format("get{%d}:%s",i,result)); } }}package com.boonya.mecache;import java.io.IOException;import java.net.InetSocketAddress;import java.util.concurrent.ExecutionException;import java.util.concurrent.Future;import net.spy.memcached.MemcachedClient;public class TestSpyMemCached { public static void main(String[] args) throws InterruptedException, ExecutionException { /** * 建立MemCached的示例 */ try { MemcachedClient memcacheClient=new MemcachedClient( new InetSocketAddress("192.168.20.37", 11211)); Future<Boolean> fbl=null; /*将key,过期时间,对应的值设入到MemCache*/ fbl=memcacheClient.set("user:data:key", 1000, "me"); if(fbl.get().booleanValue()==true){ memcacheClient.shutdown(); } } catch (IOException e) { e.printStackTrace(); } /** * 获取MemCached的示例 */ try { MemcachedClient memcacheClient=new MemcachedClient( new InetSocketAddress("192.168.20.37", 11211)); Object obj=memcacheClient.get("user:data:key"); System.out.println(obj); memcacheClient.shutdown(); } catch (IOException e) { e.printStackTrace(); } }}package com.boonya.mecache;import java.io.IOException;import java.util.concurrent.TimeoutException;import net.rubyeye.xmemcached.MemcachedClient;import net.rubyeye.xmemcached.MemcachedClientBuilder;import net.rubyeye.xmemcached.XMemcachedClientBuilder;import net.rubyeye.xmemcached.exception.MemcachedException;import net.rubyeye.xmemcached.utils.AddrUtil;public class TestXMemCached { public static void main(String[] args) { MemcachedClientBuilder builder=new XMemcachedClientBuilder( AddrUtil.getAddresses("192.168.20.37:11211")); try { MemcachedClient memCacheClient=builder.build(); try { memCacheClient.set("key", 0, "data"); String value=memCacheClient.get("key"); System.out.println("key:"+value); memCacheClient.delete("key"); value=memCacheClient.get("key"); System.out.println("key:"+value); memCacheClient.shutdown(); } catch (TimeoutException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } catch (MemcachedException e) { e.printStackTrace(); } } catch (IOException e) { e.printStackTrace(); } }}
4、使用建议
(1)、 memcached client for java不断更新,性能上得到提升,并且稳定。
(2)、 xmemcached使用广泛,其有如下特点:高性能、支持完整的协议、支持客户端分布、允许设置节点权重、动态增删节点、支持JMX、Spring框架和
Hibernate-memcached的集成、客户端连接池、可扩展性好等。