Java使用memcached

来源:互联网 发布:用王家卫 知乎 编辑:程序博客网 时间:2024/05/16 07:54

1.加载commons-pool-1.5.6.jar、java_memcached-release_2.6.6.jar、slf4j-api-1.6.1.jar、slf4j-simple-1.6.1.jar

2.创建memcached工具类:

查看源码
打印?
01public class MemcachedUtil {
02
03/**
04* memcached客户端单例
05*/
06private static MemCachedClient cachedClient = new MemCachedClient();
07
08/**
09* 初始化连接池
10*/
11static {
12//获取连接池的实例
13SockIOPool pool = SockIOPool.getInstance();
14
15//服务器列表及其权重
16String[] servers = {"127.0.0.1:11211"};
17Integer[] weights = {3};
18
19//设置服务器信息
20pool.setServers(servers);
21pool.setWeights(weights);
22
23//设置初始连接数、最小连接数、最大连接数、最大处理时间
24pool.setInitConn(10);
25pool.setMinConn(10);
26pool.setMaxConn(1000);
27pool.setMaxIdle(1000*60*60);
28
29//设置连接池守护线程的睡眠时间
30pool.setMaintSleep(60);
31
32//设置TCP参数,连接超时
33pool.setNagle(false);
34pool.setSocketTO(60);
35pool.setSocketConnectTO(0);
36
37//初始化并启动连接池
38pool.initialize();
39
40//压缩设置,超过指定大小的都压缩
41// cachedClient.setCompressEnable(true);
42// cachedClient.setCompressThreshold(1024*1024);
43}
44
45private MemcachedUtil(){
46}
47
48public static boolean add(String key, Object value) {
49return cachedClient.add(key, value);
50}
51
52public static boolean add(String key, Object value, Integer expire) {
53return cachedClient.add(key, value, expire);
54}
55
56public static boolean put(String key, Object value) {
57return cachedClient.set(key, value);
58}
59
60public static boolean put(String key, Object value, Integer expire) {
61return cachedClient.set(key, value, expire);
62}
63
64public static boolean replace(String key, Object value) {
65return cachedClient.replace(key, value);
66}
67
68public static boolean replace(String key, Object value, Integer expire) {
69return cachedClient.replace(key, value, expire);
70}
71
72public static Object get(String key) {
73return cachedClient.get(key);
74}
75
76}
3. 创建需要缓存的对象:

查看源码
打印?
01public class UserBean implements Serializable {
02
03private static final long serialVersionUID = 9174194101246733501L;
04
05private String username;
06
07private String password;
08
09public UserBean(String username, String password) {
10this.username = username;
11this.password = password;
12}
13
14public String getUsername() {
15return username;
16}
17
18public void setUsername(String username) {
19this.username = username;
20}
21
22public String getPassword() {
23return password;
24}
25
26public void setPassword(String password) {
27this.password = password;
28}
29
30@Override
31public int hashCode() {
32final int prime = 31;
33int result = 1;
34result = prime * result
35+ ((password == null) ? 0 : password.hashCode());
36result = prime * result
37+ ((username == null) ? 0 : username.hashCode());
38return result;
39}
40
41@Override
42public boolean equals(Object obj) {
43if (this == obj)
44return true;
45if (obj == null)
46return false;
47if (getClass() != obj.getClass())
48return false;
49UserBean other = (UserBean) obj;
50if (password == null) {
51if (other.password != null)
52return false;
53} else if (!password.equals(other.password))
54return false;
55if (username == null) {
56if (other.username != null)
57return false;
58} else if (!username.equals(other.username))
59return false;
60return true;
61}
62
63@Override
64public String toString() {
65return "username:" + username + ",password:" + password;
66}
67}
4.创建测试用例:

查看源码
打印?
01public class MemcachedUtilTest {
02
03@Test
04public void testMemcached() {
05MemcachedUtil.put("hello", "world", 60);
06String hello = (String) MemcachedUtil.get("hello");
07Assert.assertEquals("world", hello);
08
09for(int i = 0; i < 10000000; ++i) {
10UserBean userBean = new UserBean("Jason" + i, "123456-" + i);
11MemcachedUtil.put("user" + i, userBean, 60);
12Object obj = MemcachedUtil.get("user" + i);
13Assert.assertEquals(userBean, obj);
14}
15}
16}

5.通过spring注入memcached:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans           http://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="memcachedPool" class="com.danga.MemCached.SockIOPool" factory-method="getInstance" init-method="initialize"><constructor-arg><value>neeaMemcachedPool</value></constructor-arg><property name="servers"><list><value>127.0.0.1:11211</value></list></property><property name="initConn"><value>20</value></property><property name="minConn"><value>10</value></property><property name="maxConn"><value>50</value></property><property name="nagle"><value>false</value></property><property name="socketTO"><value>3000</value></property></bean><bean id="memcachedClient" class="com.danga.MemCached.MemCachedClient"><constructor-arg><value>neeaMemcachedPool</value></constructor-arg></bean></beans>

6.创建测试用例:

查看源码
打印?
01public class MemcachedSpringTest {
02
03private MemCachedClient cachedClient;
04
05@Before
06public void init() {
07ApplicationContext context = new ClassPathXmlApplicationContext("com/loujinhe/config/beans.xml");
08cachedClient = (MemCachedClient)context.getBean("memcachedClient");
09}
10
11@Test
12public void testMemcachedSpring() {
13UserBean user = new UserBean("lou", "jason");
14cachedClient.set("user", user);
15UserBean cachedBean = (UserBean)user;
16Assert.assertEquals(user, cachedBean);
17}
18}
来自:http://blog.csdn.net/loujinhe/article/details/8491673
原创粉丝点击