Memcached的几种Java客户端(待实践)
其实现在来尝试Memcached的客户端估计会有点过气,因为现在大势基本都在Redis那边。
Memcached Client目前有3种:
- Memcached Client for Java(已经停止更新)
- SpyMemcached(已经停止更新)
- XMemcached(一直更新,且支持客户端分片实现集群,基本现在是它的天下)
- 高性能
- 支持完整的Memcached文本协议,二进制协议。
- 支持JMX,可以通过MBean调整性能参数、动态添加/移除Server、查看统计等。
- 支持客户端统计
- 支持Memcached节点的动态增减。
- 支持Memcached分布:余数分布和一致性哈希分布。
- 更多的性能调整选项。
- XMemcached更容易与Spring集成。
- 支持客户端分片实现集群。
三种API的比较
1、较早推出的memcached JAVA客户端API,应用广泛,运行比较稳定。
2、A simple, asynchronous, single-threaded memcached client written in java. 支持异步,单线程的Memcached客户端,用到了Java1.5版本的concurrent和nio,存取速度会高于前者,但是稳定性不好,测试中常 报timeOut等相关异常。
3、XMemcached同样是基于java nio的客户端,Java nio相比于传统阻塞IO模型来说,有效率高(特别在高并发下)和资源耗费相对较少的优点。传统阻塞IO为了提高效率,需要创建一定数量的连接形成连接 池,而nio仅需要一个连接即可(当然,nio也是可以做池化处理),相对来说减少了线程创建和切换的开销,这一点在高并发下特别明显。因此XMemcached与Spymemcached在性能都非常优秀,在某些方面(存储的数据比较小的情况下)Xmemcached比Spymemcached的表现更为优秀,具体可以看这个Java Memcached Clients Benchmark。
示例代码
Memcached Client for Java
官网:https://github.com/gwhalin/Memcached-Java-Client
这个已经停止跟新。放弃使用!
import java.util.Date; import com.danga.MemCached.*; public class MemcacheManagerForGwhalin { // 构建缓存客户端 private static MemCachedClient cachedClient; // 单例模式实现客户端管理类 private static MemcacheManagerForGwhalin INSTANCE = new MemcacheManagerForGwhalin(); private MemcacheManagerForGwhalin() { cachedClient = new MemCachedClient(); // 初始化SockIOPool,管理memcached的连接池 SockIOPool pool = SockIOPool.getInstance(); // 设置缓存服务器列表,当使用分布式缓存的时,可以指定多个缓存服务器。(这里应该设置为多个不同的服务器) String[] servers = { "192.168.163.10:11211", "192.168.163.11:11211" // 也可以使用域名 "server3.mydomain.com:1624" }; pool.setServers(servers); pool.setFailover(true); pool.setInitConn(10); // 设置初始连接 pool.setMinConn(5);// 设置最小连接 pool.setMaxConn(250); // 设置最大连接 pool.setMaxIdle(1000 * 60 * 60 * 3); // 设置每个连接最大空闲时间3个小时 pool.setMaintSleep(30); pool.setNagle(false); pool.setSocketTO(3000); pool.setAliveCheck(true); pool.initialize(); } /** * 获取缓存管理器唯一实例 * * @return */ public static MemcacheManagerForGwhalin getInstance() { return INSTANCE; } public void add(String key, Object value) { cachedClient.set(key, value); } public void add(String key, Object value, int milliseconds) { cachedClient.set(key, value, milliseconds); } public void remove(String key) { cachedClient.delete(key); } public void remove(String key, int milliseconds) { cachedClient.delete(key, milliseconds, new Date()); } public void update(String key, Object value, int milliseconds) { cachedClient.replace(key, value, milliseconds); } public void update(String key, Object value) { cachedClient.replace(key, value); } public Object get(String key) { return cachedClient.get(key); } public static void main(String[] args) { //将对象加入到memcached缓存 cachedClient.add("keke", "This is a test String"); //从memcached缓存中按key值取对象 String result = (String) cachedClient.get("keke"); System.out.println(result); } }
Spymemcached
这个是不错的选择,但是有吭,在GitHub上找不到。
public class MemcacheManagerForSpy implements IMemcacheManager { // 缓存客户端 private MemcachedClient memcacheCient; // Manager管理对象,单例模式 private static MemcacheManagerForSpy INSTANCE = new MemcacheManagerForSpy(); private MemcacheManagerForSpy() { try { memcacheCient = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211)); } catch (IOException e) { e.printStackTrace(); } } public static MemcacheManagerForSpy getInstance() { return INSTANCE; } @Override public void add(String key, Object value, int milliseconds) { memcacheCient.add(key, milliseconds, value); } @Override public void add(String key, Object value) { memcacheCient.add(key, 3600, value); } @Override public void remove(String key, int milliseconds) { memcacheCient.delete(key); } @Override public void remove(String key) { memcacheCient.delete(key); } @Override public void update(String key, Object value, int milliseconds) { memcacheCient.replace(key, milliseconds, value); } @Override public void update(String key, Object value) { memcacheCient.replace(key, 3600, value); } @Override public Object get(String key) { return memcacheCient.get(key); } }
Xmemcached
官网:https://github.com/killme2008/xmemcached
目前最火,且时长更新,截止今天都还在更新。
import java.io.IOException; import java.util.concurrent.TimeoutException; import net.rubyeye.xmemcached.utils.AddrUtil; import net.rubyeye.xmemcached.MemcachedClient; import net.rubyeye.xmemcached.MemcachedClientBuilder; import net.rubyeye.xmemcached.XMemcachedClientBuilder; import net.rubyeye.xmemcached.exception.MemcachedException; public class TestXMemcache { public static void main(String[] args) { MemcachedClientBuilder builder = new XMemcachedClientBuilder(AddrUtil.getAddresses("10.11.15.222:10000")); MemcachedClient memcachedClient; try { memcachedClient = builder.build(); memcachedClient.set("hello", 0, "Hello,xmemcached"); String value = memcachedClient.get("hello"); System.out.println("hello=" + value); memcachedClient.delete("hello"); value = memcachedClient.get("hello"); System.out.println("hello=" + value); // close memcached client memcachedClient.shutdown(); } catch (MemcachedException e) { System.err.println("MemcachedClient operation fail"); e.printStackTrace(); } catch (TimeoutException e) { System.err.println("MemcachedClient operation timeout"); e.printStackTrace(); } catch (InterruptedException e) { // ignore } catch (IOException e) { System.err.println("Shutdown MemcachedClient fail"); e.printStackTrace(); } } }
参考:
http://blog.csdn.net/xuke6677/article/details/38706501
http://blog.csdn.net/hardy008/article/details/37738901
http://blog.csdn.net/arui_email/article/details/8129400
http://blog.csdn.net/heiyueya/article/details/64441901
http://exceptioneye.iteye.com/blog/1927741
http://blog.csdn.net/hengyunabc/article/details/20735701(缓存失效后的策略处理)