一、前言
本章分别通过dangamemcached包(依赖包下载)、spymemcached(依赖包下载)及xmemcached(依赖包下载)三种不同使用memcached客户端API使用方式进行对比说明,下面具体对比三种的区别
1)dangamemcached是早起实现Java api for memcached,特点:简单、稳定及后期NIO等性能版本支持不足,性能要求高,稳定性要求场景建议使用
2)spymemcached其使用基于java1.5的java.util.concurrent包对于性能提高做了考虑,特定:性能高、稳定不够
3)xmemcached基于java NIO的方式进行通信,在性能和稳定性比spymemcached优秀,在高效及并发要求的场景建议使用
二、代码示例
1.DangaMemcacheClient - 测试工具类(com.danga.MemCached.MemCachedClient、com.danga.MemCached.SockIOPool)
import com.danga.MemCached.MemCachedClient;@b@import com.danga.MemCached.SockIOPool;@b@@b@public class DangaMemcacheClient extends MemCachedClient{@b@ @b@ private String[] servers={"127.0.0.1:11211"}; @b@ private Integer[] weights={3};//权重,对应servers的index@b@ @b@ private static DangaMemcacheClient client=new DangaMemcacheClient();@b@ @b@ {@b@ SockIOPool pool = SockIOPool.getInstance(); @b@ pool.setServers(servers); @b@ pool.setWeights(weights); @b@ pool.setInitConn(5);//初始连接数@b@ pool.setMinConn(5); @b@ pool.setMaxConn(250); @b@ pool.setMaxIdle(1000*60*60*6);//连接最大空闲时间@b@ pool.setMaintSleep(30);//设置连接池维护线程的睡眠时间 @b@ pool.setNagle(false); @b@ pool.setSocketTO(3000);//设置socket的读取等待超时值 @b@ pool.setSocketConnectTO(0);//设置socket的连接等待超时值 @b@ pool.initialize();//初始化连接池@b@ }@b@ @b@ public static MemCachedClient getClient(){@b@ return client;@b@ }@b@ @b@ private DangaMemcacheClient(){@b@ super();@b@ }@b@@b@ public static void main(String[] args) {@b@ @b@ DangaMemcacheClient.getClient().set("dangacache_key","1111");@b@ @b@ System.out.println(DangaMemcacheClient.getClient().get("dangacache_key"));@b@@b@ }@b@@b@}
控制台输出:1111
2.SpymemcacheClient工具类 (net.spy.memcached.MemcachedClient)
import java.io.IOException;@b@import java.net.InetSocketAddress;@b@import net.spy.memcached.MemcachedClient;@b@@b@public class SpymemcacheClient {@b@ @b@ public static MemcachedClient getclient(){@b@ MemcachedClient $client=null;@b@ try {@b@ $client = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211));@b@ } catch (IOException e) {@b@ e.printStackTrace();@b@ }@b@ return $client;@b@ }@b@@b@ public static void main(String[] args) {@b@ @b@ SpymemcacheClient.getclient().set("spycache_key", 60000, "2222");@b@ System.out.println(SpymemcacheClient.getclient().get("spycache_key"));@b@@b@ }@b@@b@}
控制台输出:2222
3. XMemcacheClient工具类(net.rubyeye.xmemcached.*)
import java.io.IOException;@b@import net.rubyeye.xmemcached.MemcachedClient;@b@import net.rubyeye.xmemcached.MemcachedClientBuilder;@b@import net.rubyeye.xmemcached.XMemcachedClientBuilder;@b@import net.rubyeye.xmemcached.utils.AddrUtil;@b@@b@public class XMemcacheClient {@b@ @b@ public static MemcachedClient getclient(){@b@ MemcachedClient memcachedClient=null;@b@ try {@b@ MemcachedClientBuilder builder = new XMemcachedClientBuilder(AddrUtil.getAddresses("127.0.0.1:11211"));@b@ memcachedClient = builder.build();@b@ } catch (IOException e) {@b@ e.printStackTrace();@b@ }@b@ return memcachedClient;@b@ }@b@@b@ public static void main(String[] args) throws Exception{@b@ XMemcacheClient.getclient().set("xmem_key", 0, "3333");@b@ System.out.println(XMemcacheClient.getclient().get("xmem_key"));@b@ }@b@@b@}
控制台输出:3333