public class MemCacheUtil { /** * memcached客户端单例 */ private static MemcachedClient cachedClient; /** * 初始化连接池 */ static { try { MemcachedClientBuilder builder = new XMemcachedClientBuilder( AddrUtil.getAddresses("192.168.204.201:11211"), new int[] { 3 }); builder.setFailureMode(true); // 使用二进制文件 builder.setCommandFactory(new BinaryCommandFactory()); builder.setConnectionPoolSize(10); builder.setSocketOption(StandardSocketOption.SO_RCVBUF, 32 * 1024); // 设置接收缓存区为32K,默认16K builder.setSocketOption(StandardSocketOption.SO_SNDBUF, 16 * 1024); // 设置发送缓冲区为16K,默认为8K builder.setSocketOption(StandardSocketOption.TCP_NODELAY, false); // 启用nagle算法,提高吞吐量,默认关闭 builder.setOpTimeout(10000);// 设置超时为10秒 cachedClient = builder.build(); } catch (Exception e) { // TODO: handle exception } } /** * @throws MemcachedException * @throws InterruptedException * @throws TimeoutException * * @Title: add * @Description: 添加一个值,如果已经存在,则返回false * @param key 键 * @param value 值 * @return * @throws */ public static boolean add(String key, Object value) throws TimeoutException, InterruptedException, MemcachedException { return cachedClient.add(key, 0, value); } /** * @throws MemcachedException * @throws InterruptedException * @throws TimeoutException * * @Title: add * @Description: 添加一个值,如果已经存在,则返回false * @param key 键 * @param value 值 * @param expire 存活期 * @return * @throws */ public static boolean add(String key, Object value, Integer expire) throws TimeoutException, InterruptedException, MemcachedException { return cachedClient.add(key, expire, value); } /** * @throws MemcachedException * @throws InterruptedException * @throws TimeoutException * * @Title: put * @Description: 向Memcache添加一个值,如果已经存在,则覆写 * @param key 键 * @param value 值 * @return * @throws */ public static boolean put(String key, Object value) throws TimeoutException, InterruptedException, MemcachedException { return cachedClient.set(key, 0, value); } /** * @throws MemcachedException * @throws InterruptedException * @throws TimeoutException * * @Title: put * @Description: 向Memcache添加一个值,如果已经存在,则覆写 * @param key 键 * @param value 值 * @param expire 存活期 * @return * @throws */ public static boolean put(String key, Object value, Integer expire) throws TimeoutException, InterruptedException, MemcachedException { return cachedClient.set(key, expire, value); } /** * @throws MemcachedException * @throws InterruptedException * @throws TimeoutException * * @Title: replace * @Description: 替换一个已经存在Memcache服务器上的项目 * @param key 键 * @param value 值 * @return * @throws */ public static boolean replace(String key, Object value) throws TimeoutException, InterruptedException, MemcachedException { return cachedClient.replace(key, 0, value); } /** * @throws MemcachedException * @throws InterruptedException * @throws TimeoutException * * @Title: replace * @Description: 替换一个已经存在Memcache服务器上的项目 * @param key 键 * @param value 值 * @param expire 存活期 * @return * @throws */ public static boolean replace(String key, Object value, Integer expire) throws TimeoutException, InterruptedException, MemcachedException { return cachedClient.replace(key, expire, value); } /** * @throws MemcachedException * @throws InterruptedException * @throws TimeoutException * * @Title: get * @Description: 从Memcache上获取一个key值 * @param key 键 * @return * @throws */ public static Object get(String key) throws TimeoutException, InterruptedException, MemcachedException { return cachedClient.get(key); } /** * @throws MemcachedException * @throws InterruptedException * @throws TimeoutException * * @Title: delete * @Description: 删除一个Memcache上的key值 * @param key 键 * @return * @throws */ public static boolean delete(String key) throws TimeoutException, InterruptedException, MemcachedException { return cachedClient.delete(key); } }
相关推荐
xmemcached-1.2.4的官方源码。 xmemcached XMemcached is a high performance, easy to use blocking multithreaded memcached client in java. It's nio based (using my opensource nio framework :yanf4j), ...
repository\com\googlecode\xmemcached\xmemcached 2.4.6
xmemcached-1.2.6.2
memcache 分布式缓存,客户端,速度虽不比ehcache快。
xmemcached使用的jar,需要的可以下载
xmemcached中文开发手册,提供的版本较老,但针对2.0的版本次文档同样适用,可以入门
xmemcached.chm帮助文档
xmemcached-1.4.3.jar
spring+xmemcached aop切面 需要xmemcached-1.2.5+spring-2.5.6 class="net.rubyeye.xmemcached.utils.XMemcachedClientFactoryBean" destroy-method="shutdown"> ${XMemcached_servers} <!-- ...
测试类包括Xmemcached客户端与memcached client for java两者,可运行比较性能。 XMemcached简介: XMemcached是基于 java nio的Memcached客户端,java nio相比于传统阻塞 io 模型来说,有 效率高(特别在高并发下...
memcached 客户端 xmemcached jar 包,xmemcached 源文件 及 api
xmemcached 接口文档,方便开发和学习
基于java nio实现的高性能可扩展的memcached客户端。虽然Java的memcached库已经很多,但是这些Java开源memcached库并没有一个是基于NIO框架编写,因此...xmemcached的项目主页在:http://code.google.com/p/xmemcached/
xmemcached-1.4.2最新版,可用。memcached java客户端
Memcached 是一个高性能的分布式内存对象的key-value缓存系统,用于动态Web应用以减轻数据库负载,现在也有很多人将它作为内存式数据库在使用,memcached通过它的自定义协议与客户端交互,而XMemcached就是它的一个...
xmemcached 2.4.5 和 xmemcached 2.3.2 两个版本号的
xmemcached1.3.5源码-附带自己写的RMI调用它的JMX服务,使用RMI调用JMX服务的详细过程,完整的eclipse工程,直接导入即可用。还用一些运行截图,很有用。 自己写的例子,类名是BaseExample 和RMITest.
官方中文手册,各种开发实例,集成spring等详尽配置信息,不错的资料!