Java使用memcached
- - 互联网 - ITeye博客c:>memcached.exe -l 127.0.0.1 -m 32 -d start.
首先到 http://danga.com/memcached下载memcached的windows版本和java客户端jar包,目前最新版本是memcached-1.2.1-win32.zip和java_memcached-release_1.6.zip,分别解压后即可!
然后是安装运行memcached服务器,我们将memcached-1.2.1-win32.zip解压后,进入其目录,然后运行如下命令:c:>;memcached.exe -d install<br>c:>memcached.exe -l 127.0.0.1 -m 32 -d start
第一行是安装memcached成为服务,这样才能正常运行,否则运行失败!
第一行是启动memcached的,作为测试我们就简单的只分配32M内存了,然后监听本机端口和以守护进行运行。执行完毕后,我们就可以在任务管理器中见到memcached.exe这个进程了。
好了,我们的服务器已经正常运行了, 下面我们就来写java的客户端连接程序。
我们将java_memcached-release_1.6.zip解压后的目录中的java_memcached-release_1.6.jar文件复制到java项目的lib目录下,然后我们来编写代码,比如我提供的一个应用类如下:
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工具类:
01 |
public class MemcachedUtil { |
02 |
03 |
/**
|
04 |
* memcached客户端单例
|
05 |
*/
|
06 |
private static MemCachedClient cachedClient = new MemCachedClient();
|
07 |
|
08 |
/**
|
09 |
* 初始化连接池
|
10 |
*/
|
11 |
static {
|
12 |
//获取连接池的实例
|
13 |
SockIOPool pool = SockIOPool.getInstance();
|
14 |
|
15 |
//服务器列表及其权重
|
16 |
String[] servers = {"127.0.0.1:11211"};
|
17 |
Integer[] weights = {3};
|
18 |
|
19 |
//设置服务器信息
|
20 |
pool.setServers(servers);
|
21 |
pool.setWeights(weights);
|
22 |
|
23 |
//设置初始连接数、最小连接数、最大连接数、最大处理时间
|
24 |
pool.setInitConn(10);
|
25 |
pool.setMinConn(10);
|
26 |
pool.setMaxConn(1000);
|
27 |
pool.setMaxIdle(1000*60*60);
|
28 |
|
29 |
//设置连接池守护线程的睡眠时间
|
30 |
pool.setMaintSleep(60);
|
31 |
|
32 |
//设置TCP参数,连接超时
|
33 |
pool.setNagle(false);
|
34 |
pool.setSocketTO(60);
|
35 |
pool.setSocketConnectTO(0);
|
36 |
|
37 |
//初始化并启动连接池
|
38 |
pool.initialize();
|
39 |
|
40 |
//压缩设置,超过指定大小的都压缩
|
41 |
// cachedClient.setCompressEnable(true); |
42 |
// cachedClient.setCompressThreshold(1024*1024); |
43 |
}
|
44 |
|
45 |
private MemcachedUtil(){
|
46 |
}
|
47 |
|
48 |
public static boolean add(String key, Object value) {
|
49 |
return cachedClient.add(key, value);
|
50 |
}
|
51 |
|
52 |
public static boolean add(String key, Object value, Integer expire) {
|
53 |
return cachedClient.add(key, value, expire);
|
54 |
}
|
55 |
|
56 |
public static boolean put(String key, Object value) {
|
57 |
return cachedClient.set(key, value);
|
58 |
}
|
59 |
|
60 |
public static boolean put(String key, Object value, Integer expire) {
|
61 |
return cachedClient.set(key, value, expire);
|
62 |
}
|
63 |
|
64 |
public static boolean replace(String key, Object value) {
|
65 |
return cachedClient.replace(key, value);
|
66 |
}
|
67 |
|
68 |
public static boolean replace(String key, Object value, Integer expire) {
|
69 |
return cachedClient.replace(key, value, expire);
|
70 |
}
|
71 |
|
72 |
public static Object get(String key) {
|
73 |
return cachedClient.get(key);
|
74 |
}
|
75 |
|
76 |
} |
3. 创建需要缓存的对象:
01 |
public class UserBean implements Serializable { |
02 |
03 |
private static final long serialVersionUID = 9174194101246733501L;
|
04 |
05 |
private String username;
|
06 |
|
07 |
private String password;
|
08 |
|
09 |
public UserBean(String username, String password) {
|
10 |
this.username = username;
|
11 |
this.password = password;
|
12 |
}
|
13 |
|
14 |
public String getUsername() {
|
15 |
return username;
|
16 |
}
|
17 |
|
18 |
public void setUsername(String username) {
|
19 |
this.username = username;
|
20 |
}
|
21 |
|
22 |
public String getPassword() {
|
23 |
return password;
|
24 |
}
|
25 |
|
26 |
public void setPassword(String password) {
|
27 |
this.password = password;
|
28 |
}
|
29 |
|
30 |
@Override
|
31 |
public int hashCode() {
|
32 |
final int prime = 31;
|
33 |
int result = 1;
|
34 |
result = prime * result
|
35 |
+ ((password == null) ? 0 : password.hashCode());
|
36 |
result = prime * result
|
37 |
+ ((username == null) ? 0 : username.hashCode());
|
38 |
return result;
|
39 |
}
|
40 |
41 |
@Override
|
42 |
public boolean equals(Object obj) {
|
43 |
if (this == obj)
|
44 |
return true;
|
45 |
if (obj == null)
|
46 |
return false;
|
47 |
if (getClass() != obj.getClass())
|
48 |
return false;
|
49 |
UserBean other = (UserBean) obj;
|
50 |
if (password == null) {
|
51 |
if (other.password != null)
|
52 |
return false;
|
53 |
} else if (!password.equals(other.password))
|
54 |
return false;
|
55 |
if (username == null) {
|
56 |
if (other.username != null)
|
57 |
return false;
|
58 |
} else if (!username.equals(other.username))
|
59 |
return false;
|
60 |
return true;
|
61 |
}
|
62 |
63 |
@Override
|
64 |
public String toString() {
|
65 |
return "username:" + username + ",password:" + password;
|
66 |
}
|
67 |
} |
4.创建测试用例:
01 |
public class MemcachedUtilTest { |
02 |
03 |
@Test
|
04 |
public void testMemcached() {
|
05 |
MemcachedUtil.put("hello", "world", 60);
|
06 |
String hello = (String) MemcachedUtil.get("hello");
|
07 |
Assert.assertEquals("world", hello);
|
08 |
|
09 |
for(int i = 0; i < 10000000; ++i) {
|
10 |
UserBean userBean = new UserBean("Jason" + i, "123456-" + i);
|
11 |
MemcachedUtil.put("user" + i, userBean, 60);
|
12 |
Object obj = MemcachedUtil.get("user" + i);
|
13 |
Assert.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.创建测试用例:
01 |
public class MemcachedSpringTest { |
02 |
03 |
private MemCachedClient cachedClient;
|
04 |
|
05 |
@Before
|
06 |
public void init() {
|
07 |
ApplicationContext context = new ClassPathXmlApplicationContext("com/loujinhe/config/beans.xml");
|
08 |
cachedClient = (MemCachedClient)context.getBean("memcachedClient");
|
09 |
}
|
10 |
|
11 |
@Test
|
12 |
public void testMemcachedSpring() {
|
13 |
UserBean user = new UserBean("lou", "jason");
|
14 |
cachedClient.set("user", user);
|
15 |
UserBean cachedBean = (UserBean)user;
|
16 |
Assert.assertEquals(user, cachedBean);
|
17 |
}
|
18 |
} |