Memcached 安装配置 & 基本指令
Java 客户端连接 Memcached & Spring 集成
public class MemcachedJavaTest {
private final static Logger log = LogManager.getLogger();
public static void main(String[] args) throws IOException, ExecutionException, InterruptedException {
// 创建 memcached 客户端
MemcachedClient client = new MemcachedClient(new InetSocketAddress("127.0.0.1",11211));
// 演示各种 memcached 操作
// set 操作
Future fo = client.set("key1",500,"Hello world");
log.debug("set result: " + fo.get());
// get 操作
String key1 = (String) client.get("key1");
log.debug("key1: " + key1);
//del 操作
fo = client.delete("key1");
log.debug("delete result: " + fo.get());
// add 操作
fo = client.add("key2",500,"Are you OK?");
log.debug("add result: " + fo.get());
// gets, cas 操作
CASValue cas = client.gets("key2"); //获取 key2 的 cas 令牌
client.cas("key2",cas.getCas(),"Deep Dark Fantatic");
log.debug("key2: " + client.get("key2"));
// append 操作
cas = client.gets("key2");
client.append(cas.getCas(),"key2","!♂ I'm Van ♂");
log.debug("key2: " + client.get("key2"));
// 演示读写 Java Bean
User user = new User();
user.setId(123L);
user.setName("assad");
user.setAge(22);
user.setCity("Guangzhou");
// 写入 Java Bean (写入Java Bean 的序列化字节)
client.set(user.getId()+"",500,user);
// 获取 Java Bean (读取的时字节值并反序列化为 Java Bean)
User userGet = (User) client.get(123L+"");
log.debug("user: " + userGet);
// 关闭 memcached 客户端
client.shutdown();
}
static class User implements Serializable{
private static final long serialVersionUID = 2333L;
private long id ;
private String name;
private int age;
private String city;
//getter,setter,toString
}
}
16:46:07.585 [main] DEBUG memcached.SpyMemcachedTest - set result: true
16:46:07.591 [main] DEBUG memcached.SpyMemcachedTest - key1: Hello world
16:46:07.593 [main] DEBUG memcached.SpyMemcachedTest - delete result: true
16:46:07.594 [main] DEBUG memcached.SpyMemcachedTest - add result: false
16:46:07.599 [main] DEBUG memcached.SpyMemcachedTest - key2: Deep Dark Fantatic
16:46:07.603 [main] DEBUG memcached.SpyMemcachedTest - key2: Deep Dark Fantatic!♂ I'm Van ♂
16:46:07.614 [main] DEBUG memcached.SpyMemcachedTest - user: User{id=123, name='assad', age=22, city='Guangzhou'}
<beans ... >
<bean id="memcachedClient" class="net.spy.memcached.spring.MemcachedClientFactoryBean" destroy-method="destroy">
<property name="servers" value="127.0.0.1:1121" />
<property name="failureMode" value="Redistribute" />
<property name="maxReconnectDelay" value="3" />
<property name="protocol" value="BINARY" />
<property name="transcoder">
<bean class="net.spy.memcached.transcoders.SerializingTranscoder" p:compressionThreshold="1024" p:charset="UTF-8" />
property>
<property name="opTimeout" value="5000" />
<property name="locatorType" value="CONSISTENT" />
<property name="useNagleAlgorithm" value="false" />
bean>
beans>
public class XmencachedTest {
private static final Logger log = LogManager.getLogger();
public static void main(String[] args) throws IOException, InterruptedException, MemcachedException, TimeoutException {
// 创建 memcached 客户端
XMemcachedClient client = new XMemcachedClient(new InetSocketAddress("127.0.0.1",11211));
// 演示各种 memcached 操作
// set 操作
boolean result = client.set("key1",500,"Hello world");
log.debug("set result: " + result);
// get 操作
String key1 = (String) client.get("key1");
log.debug("key1: " + key1);
//del 操作
result = client.delete("key1");
log.debug("delete result: " + result);
// add 操作
result = client.add("key2",500,"Are you OK?");
log.debug("add result: " + result);
// gets, cas 操作
long cas = client.gets("key2").getCas();
client.cas("key2",500,"Deep Dark Fantatic",cas);
log.debug("key2: " + client.get("key2"));
// append 操作
client.append("key2","!♂ I'm Van ♂");
log.debug("key2: " + client.get("key2"));
// 演示读写 Java Bean
User user = new User();
user.setId(123L);
user.setName("assad");
user.setAge(22);
user.setCity("Guangzhou");
// 写入 Java Bean
client.set(user.getId()+"",500,user);
// 获取 Java Bean
User userGet = client.get(123L+"");
log.debug("user: " + userGet);
client.shutdown();
}
static class User implements Serializable {
private static final long serialVersionUID = 2333L;
private long id ;
private String name;
private int age;
private String city;
//getter,setter, toString
}
}
<beans ...>
<bean id="xMemcachedBuilder" class="net.rubyeye.xmemcached.XMemcachedClientBuilder">
<property name="connectionPoolSize" value="100" />
<property name="opTimeout" value="5000" />
<property name="failureMode" value="true" />
<property name="commandFactory">
<bean class="net.rubyeye.xmemcached.command.BinaryCommandFactory" />
property>
<property name="sessionLocator">
<bean class="net.rubyeye.xmemcached.impl.KetamaMemcachedSessionLocator" />
property>
<property name="transcoder">
<bean class="net.rubyeye.xmemcached.transcoders.SerializingTranscoder" />
property>
bean>
<bean id="memcachedClient" factory-bean="xMemcachedBuilder" factory-method="build" destroy-method="shutdown" />
beans>