memcache:http://memcached.org/
github地址:https://github.com/killme2008/xmemcached
其他客户端:
https://code.google.com/archive/p/spymemcached/
http://github.com/gwhalin/Memcached-Java-Client
我本地安装了个windows版的memcached客户端,并添加为windows服务。
添加maven包:
<dependency>
<groupId>com.googlecode.xmemcached</groupId>
<artifactId>xmemcached</artifactId>
<version>2.0.0</version>
</dependency>
做个简单的testcase:
package com.raycloud.maijia.schedule.utils;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
import net.rubyeye.xmemcached.MemcachedClient;
import net.rubyeye.xmemcached.XMemcachedClient;
import net.rubyeye.xmemcached.exception.MemcachedException;
import org.junit.Test;
public class TestMemcached {
@Test
public void testIt(){
try {
MemcachedClient client = new XMemcachedClient("127.0.0.1", 11556);
String testVal = "this is a test demo.";
client.set("test_key", 10, testVal);
Thread.sleep(5000);
System.out.println("=================" + client.get("test_key"));
Thread.sleep(15000);
System.out.println("=================" + client.get("test_key"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TimeoutException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MemcachedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
输出结果:
13:51:57.904 [Xmemcached-Reactor-2] DEBUG c.g.c.y.core.impl.AbstractSession - read 64 bytes from channel
=================this is a test demo.
13:52:02.906 [Xmemcached-Reactor-2] DEBUG n.r.xmemcached.impl.MemcachedHandler - Check session (%s) is alive,send heartbeat
13:52:02.907 [Xmemcached-Reactor-2] DEBUG c.g.c.y.core.impl.AbstractSession - After encodingversion
13:52:02.908 [Xmemcached-Reactor-2] DEBUG n.rubyeye.xmemcached.impl.Optimizer - Optimieze merge buffer:version
13:52:02.908 [Xmemcached-Reactor-2] DEBUG c.g.c.y.core.impl.AbstractSession - send buffers:
[
buffer:position=0,limit=9,capacity=9
]
13:52:02.910 [Xmemcached-Reactor-2] DEBUG c.g.c.y.core.impl.AbstractSession - read 16 bytes from channel
13:52:07.911 [Xmemcached-Reactor-2] DEBUG n.r.xmemcached.impl.MemcachedHandler - Check session (%s) is alive,send heartbeat
13:52:07.912 [Xmemcached-Reactor-2] DEBUG c.g.c.y.core.impl.AbstractSession - After encodingversion
13:52:07.912 [Xmemcached-Reactor-2] DEBUG n.rubyeye.xmemcached.impl.Optimizer - Optimieze merge buffer:version
13:52:07.912 [Xmemcached-Reactor-2] DEBUG c.g.c.y.core.impl.AbstractSession - send buffers:
[
buffer:position=0,limit=9,capacity=9
]
13:52:07.924 [Xmemcached-Reactor-2] DEBUG c.g.c.y.core.impl.AbstractSession - read 16 bytes from channel
13:52:12.906 [main] DEBUG c.g.c.y.core.impl.AbstractSession - After encodingget test_key
13:52:12.907 [Xmemcached-Reactor-2] DEBUG n.rubyeye.xmemcached.impl.Optimizer - Optimieze merge buffer:get test_key
13:52:12.908 [Xmemcached-Reactor-2] DEBUG c.g.c.y.core.impl.AbstractSession - send buffers:
[
buffer:position=0,limit=14,capacity=14
]
13:52:12.909 [Xmemcached-Reactor-2] DEBUG c.g.c.y.core.impl.AbstractSession - read 5 bytes from channel
=================null
spring配置:
<bean name="memcachedClient" class="net.rubyeye.xmemcached.utils.XMemcachedClientFactoryBean" destroy-method="shutdown">
<property name="servers">
<value>${memcached.url}</value>
</property>
<property name="commandFactory">
<bean class="net.rubyeye.xmemcached.command.BinaryCommandFactory"></bean>
</property>
</bean>
此处注意对象的比较和变量的比较
其他:
memcached客户端集群,以及使用repcached补丁实现主从复制:
http://sourceforge.net/projects/repcached/
还可以使用代理服务:http://code.google.com/p/memagent/
常见问题:
一、net.rubyeye.xmemcached.exception.MemcachedException: Binary protocol doesn't support iterating all keys in memcached
二进制协议不支持遍历key,文本协议支持(memcache不建议遍历key,后续版本可能会下线)
KeyIterator it = memcachedClient.getKeyIterator(new
InetSocketAddress("127.0.0.1", 11556));
while(it.hasNext()){
String key = it.next();
logger.info("=========key========" + key);
}