Pom文件pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.citi.learn</groupId>
<artifactId>learn</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>learn-memcached</artifactId>
<packaging>war</packaging>
<name>learn-memcached</name>
<description>learn-memcached</description>
<properties>
<org.springframework.version>3.2.3.RELEASE</org.springframework.version>
</properties>
<dependencies>
<!-- junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!-- memcached -->
<!-- <dependency> <groupId>com.google.code.maven-play-plugin.spy</groupId>
<artifactId>memcached</artifactId> <version>2.4.2</version> </dependency> -->
<dependency>
<groupId>com.googlecode.xmemcached</groupId>
<artifactId>xmemcached</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies>
</project>
memcached.properties文件
#连接池大小即客户端个数
memcached.connectionPoolSize=50
#宕机报警
memcached.failureMode=true
#server1
memcached.server1.host=vm-09b7-b009.apac.nsroot.net
memcached.server1.port=11211
memcached.server1.weight=4
#server2
memcached.server2.host=10.11.155.41
memcached.server2.port=11211
memcached.server2.weight=3
#server3
memcached.server3.host=10.10.76.31
memcached.server3.port=11211
memcached.server3.weight=2
#server4
memcached.server4.host=10.10.76.35
memcached.server4.port=11211
memcached.server4.weight=1
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<context:property-placeholder location="classpath*:memcached.properties" />
<bean name="memcachedClient"
class="net.rubyeye.xmemcached.utils.XMemcachedClientFactoryBean"
destroy-method="shutdown">
<property name="servers">
<value>
${memcached.server1.host}:${memcached.server1.port}
<!-- ${memcached.server2.host}:${memcached.server2.port} ${memcached.server3.host}:${memcached.server3.port}
${memcached.server4.host}:${memcached.server4.port} -->
</value>
</property>
<!-- server's weights -->
<property name="weights">
<list>
<value>${memcached.server1.weight}</value>
<!-- <value>${memcached.server2.weight}</value> <value>${memcached.server3.weight}</value>
<value>${memcached.server4.weight}</value> -->
</list>
</property>
<property name="failureMode" value="${memcahed.failureMode}"></property>
<!-- nio connection pool size -->
<property name="connectionPoolSize" value="${memcached.connectionPoolSize}" />
<!--使用二进制文件模式 Use binary protocol,default is TextCommandFactory -->
<property name="commandFactory">
<bean class="net.rubyeye.xmemcached.command.BinaryCommandFactory"></bean>
</property>
<!-- 使用一致性哈希算法 Distributed strategy -->
<property name="sessionLocator">
<bean class="net.rubyeye.xmemcached.impl.KetamaMemcachedSessionLocator"></bean>
</property>
<!-- 使用序列化传输编码 Serializing transcoder -->
<property name="transcoder">
<bean class="net.rubyeye.xmemcached.transcoders.SerializingTranscoder" />
</property>
<!-- ByteBuffer allocator -->
<property name="bufferAllocator">
<bean class="net.rubyeye.xmemcached.buffer.SimpleBufferAllocator"></bean>
</property>
</bean>
</beans>
测试类
package com.memcached.test;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import net.rubyeye.xmemcached.MemcachedClient;
import net.rubyeye.xmemcached.MemcachedClientBuilder;
import net.rubyeye.xmemcached.XMemcachedClientBuilder;
import net.rubyeye.xmemcached.command.BinaryCommandFactory;
import net.rubyeye.xmemcached.exception.MemcachedException;
import net.rubyeye.xmemcached.utils.AddrUtil;
public class MemcachedClientTest {
private ApplicationContext ctx;
private MemcachedClient memcachedClient;
@Before
public void init() {
ctx = new ClassPathXmlApplicationContext("spring_commons.xml");
memcachedClient = (MemcachedClient) ctx.getBean("memcachedClient");
}
@Test
public void test() {
try {
// 设置/获取
memcachedClient.set("zlex", 36000, "set/get");
assertEquals("set/get", memcachedClient.get("zlex"));
// 替换
memcachedClient.replace("zlex", 36000, "replace");
assertEquals("replace", memcachedClient.get("zlex"));
// 移除
memcachedClient.delete("zlex");
assertNull(memcachedClient.get("zlex"));
} 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();
} finally {
if (memcachedClient != null) {
try {
memcachedClient.shutdown();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
-
- MemcachedClientBuilder核心接口,用来控制Client的构建(build()方法)和关闭(shutdown()方法)。 MemcachedClient 由spring注入,不需要重复调用build()跟shutdown()方法,这些操作交给Spring来完成。
- MemcachedClientBuilder核心接口,用来控制Client的构建(build()方法)和关闭(shutdown()方法)。 MemcachedClient 由spring注入,不需要重复调用build()跟shutdown()方法,这些操作交给Spring来完成。
总结
- Memcached的Key,要杜绝使用空格,且长度控制在250个字符。
- Memcached的Value,要控制体积,必须小于1MB,必要时进行使用压缩。
- 失效时间,0为永久有效,最大值不得超过30天(2592000s),否则重新计算可能缓存只有1秒
- Memcached仅支持LRU算法,完全适用你的需要。
- 尽量不要将List这种重体积对象扔到Memcached中,传输、存储都会产生瓶颈。
- 使用一致性哈希算法实现,提高多个Memcacehd Server利用率。