http://jehiah.cz/projects/memcached-win32/files/memcached-1.2.1-win32.zip
这个用讯雷可以下载下来!
package org.rui.mem;
import com.danga.MemCached.MemCachedClient;
import com.danga.MemCached.SockIOPool;
public class MemcachedUtil {
/**
* memcached客户端单例
*/
private static MemCachedClient cachedClient = new MemCachedClient();
/**
* 初始化连接池
*/
static {
//获取连接池的实例
SockIOPool pool = SockIOPool.getInstance();
//服务器列表及其权重
String[] servers = {"127.0.0.1:11211"};
Integer[] weights = {3};
//设置服务器信息
pool.setServers(servers);
pool.setWeights(weights);
//设置初始连接数、最小连接数、最大连接数、最大处理时间
pool.setInitConn(10);
pool.setMinConn(10);
pool.setMaxConn(1000);
pool.setMaxIdle(1000*60*60);
//设置连接池守护线程的睡眠时间
pool.setMaintSleep(60);
//设置TCP参数,连接超时
pool.setNagle(false);
pool.setSocketTO(60);
pool.setSocketConnectTO(0);
//初始化并启动连接池
pool.initialize();
//压缩设置,超过指定大小的都压缩
// cachedClient.setCompressEnable(true);
// cachedClient.setCompressThreshold(1024*1024);
}
private MemcachedUtil(){
}
public static boolean add(String key, Object value) {
return cachedClient.add(key, value);
}
public static boolean add(String key, Object value, Integer expire) {
return cachedClient.add(key, value, expire);
}
public static boolean put(String key, Object value) {
return cachedClient.set(key, value);
}
public static boolean put(String key, Object value, Integer expire) {
return cachedClient.set(key, value, expire);
}
public static boolean replace(String key, Object value) {
return cachedClient.replace(key, value);
}
public static boolean replace(String key, Object value, Integer expire) {
return cachedClient.replace(key, value, expire);
}
public static Object get(String key) {
return cachedClient.get(key);
}
}package org.rui.mem;
import java.io.Serializable;
public class UserBean implements Serializable {
private static final long serialVersionUID = 9174194101246733501L;
private String username;
private String password;
public UserBean(String username, String password) {
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((password == null) ? 0 : password.hashCode());
result = prime * result
+ ((username == null) ? 0 : username.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
UserBean other = (UserBean) obj;
if (password == null) {
if (other.password != null)
return false;
} else if (!password.equals(other.password))
return false;
if (username == null) {
if (other.username != null)
return false;
} else if (!username.equals(other.username))
return false;
return true;
}
@Override
public String toString() {
return "username:" + username + ",password:" + password;
}
}package org.rui.mem;
import org.junit.Assert;
import org.junit.Test;
public class MemcachedUtilTest {
@Test
public void testMemcached() {
MemcachedUtil.put("hello", "world", 60);
String hello = (String) MemcachedUtil.get("hello");
// System.out.println(hello);
Assert.assertEquals("world", hello);
for(int i = 0; i < 100; ++i) {
UserBean userBean = new UserBean("code-" + i, "pass-" + i);
MemcachedUtil.put("token-" + i, userBean, 60);
Object obj = MemcachedUtil.get("token-" + i);
Assert.assertEquals(userBean, obj);
}
}
}目录<a target="_blank" " title="系统根据文章中H1到H6标签自动生成文章目录" style="color:rgb(51,102,153); text-decoration:none">(?)[-]
针对Memcached官方网站
提供的java_memcached-release_2.0.1版本进行阅读分析,Memcached
Java客户端lib库主要提供的调用类是SockIOPool和MemCachedClient?,关键类及方法整理说明如下。
这个类用来创建管理客户端和服务器通讯连接池,客户端主要的工作包括数据通讯、服务器定位、hash码生成等都是由这个类完成的。
for ( int i = 0; i < servers.length; i+/+ ) { if ( this.weights /!= null && this.weights.length > i ) { for ( int k = 0; k < this.weights[i].intValue(); k+/+ ) { this.buckets.add( servers[i] ); if ( log.isDebugEnabled() ) log.debug( "++++ added " + servers[i] + " to server bucket" ); } }
有些应用情况下,需要遍历memcached服务器中所有被cache的数据,目前memcached client API不支持遍历操作,需要进行扩展。
当memcached被用作session服务器的时候,需要支持session的access方法,根据最近访问时间刷新过期时间,目前memcached也不支持该操作,需要进行扩展。
原文地址:http://blog.csdn.net/liangrui1988/article/details/41788377