介绍
memcached java client是官方推荐的最早的memcached java客户端。最新版本:java_memcached-release_2.6.1。
官方下载地址:https://github.com/gwhalin/memcached-java-client
采用阻塞式socket通讯,据说目前版本进行了很多优化,性能有所提高(只看过1.5的源代码,还没来及看最新的)
提供key方式的连接池,默认连接池key为default。(老版本了)。2.6.1版本支持apache-commoms-pool作为连接池。
支持权重配置。
后期的版本提增加了cas支持和getmutl功能
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIml2ZukHcvN2Xu92Yp9CXzV2Zh1WavwVbvNmLllXZ0lmL5x2bvNWYvw1LcpDc0RHaiojIsJye.gif)
import com.danga.memcached.memcachedclient;
import com.danga.memcached.sockiopool;
import com.schooner.memcached.memcacheditem;
public class memcachedforjavaexample {
// create a static client as most installs only need
// a single instance
protected static memcachedclient mcc = new memcachedclient();
// set up connection pool once at class load
static {
// server list and weights
string[] servers = { "localhost:11211", "localhost:11212", "localhost:11213" };
integer[] weights = { 3, 3, 2 };
// grab an instance of our connection pool
sockiopool pool = sockiopool.getinstance();
// set the servers and the weights
pool.setservers(servers);
pool.setweights(weights);
pool.sethashingalg(sockiopool.consistent_hash);
// set some basic pool settings
// 5 initial, 5 min, and 250 max conns
// and set the max idle time for a conn
// to 6 hours
pool.setinitconn(5);
pool.setminconn(5);
pool.setmaxconn(250);
pool.setmaxidle(1000 * 60 * 60 * 6);
// set the sleep for the maint thread
// it will wake up every x seconds and
// maintain the pool size
pool.setmaintsleep(30);
// set some tcp settings
// disable nagle
// set the read timeout to 3 secs
// and don't set a connect timeout
pool.setnagle(false);
pool.setsocketto(3000);
pool.setsocketconnectto(0);
// initialize the connection pool
pool.initialize();
}
public static void main(string[] args) {
system.out.println("set: " + mcc.set("key1", "value1"));
system.out.println("set: " + mcc.set("key2", "value2"));
system.out.println("set: " + mcc.set("key3", "value3"));
system.out.println("get: " + mcc.get("key1"));
memcacheditem item = mcc.gets("key1");
system.out.println("gets: value=" + item.getvalue() + ",casunique:"+item.getcasunique());
system.out.println("set: " + mcc.set("key1", "value1_1"));
system.out.println("cas: " + mcc.cas("key1", "value1_2", item.getcasunique())); //必须false
system.out.println("getmulti:" + mcc.getmulti(new string[]{"key1","key2","key3"}));
}
这个标题不好取,因为是我自己的想法,还需要大家多提意见,一起讨论。想叫“建议代码”或者“推荐代码”,觉得不合适,还是先叫“我的代码”吧,呵呵。
1. 在原始客户端上层,根据业务需求封装memcachedservice(或叫memcachedclient),负责缓存功能的包装。如:你的业务只需要add,set,get,gets,cas,delete业务,那就只封装这几个功能。这样做的好处是,屏蔽了各种客户端的api差异,让你的业务系统与客户端实现解耦合,如果你以后需要换客户端实现,对你的业务系统不会照成影响。
2. 一般不要直接采用new的方式在你的代码中显示使用memcached客户端实现,应该采用单例的方式使用memcached客户端实现,或者使用spring的singleton方式配置。memcached客户端实现是线程安全的。
3. memcached客户端一般都需要大量的配置,考虑扩展和配置修改,应该把参数设置设计为可配置的,可以写到propertis配置文件中或是使用spring进行配置。
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIml2ZukHcvN2Xu92Yp9CXzV2Zh1WavwVbvNmLllXZ0lmL5x2bvNWYvw1LcpDc0RHaiojIsJye.gif)
/**
* memcached 常用功能接口定义,用于业务层直接使用,屏蔽各种客户端实现的api差异,实现解耦客户端与业务系统的目的
* 无过期时间和flags支持,无append,prepend,replace,incr,decr等操作
*
* @author zhangpu
*/
public interface memcachedclientservice {
string get(string key);
cacheitem gets(string key);
boolean add(string key, string value);
boolean set(string key, string value);
boolean cas(string key, string value, long unique);
boolean delete(string key)
boolean flushall();
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIml2ZukHcvN2Xu92Yp9CXzV2Zh1WavwVbvNmLllXZ0lmL5x2bvNWYvw1LcpDc0RHaiojIsJye.gif)
public class cacheitem {
private string key;
private string value;
private long unique;
public cacheitem() {
super();
public cacheitem(string key, string value, long unique) {
this.key = key;
this.value = value;
this.unique = unique;
public string getkey() {
return key;
public void setkey(string key) {
public string getvalue() {
return value;
public void setvalue(string value) {
public long getunique() {
return unique;
public void setunique(long unique) {
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIml2ZukHcvN2Xu92Yp9CXzV2Zh1WavwVbvNmLllXZ0lmL5x2bvNWYvw1LcpDc0RHaiojIsJye.gif)
* memcached for java客户端缓存服务实现
*
public class memcachedclientjava implements memcachedclientservice {
memcachedclient mmc = memcachedclientfactory.getinstance();
public boolean add(string key, string value) {
return mmc.add(key, value);
public boolean cas(string key, string value, long unique) {
return mmc.cas(key, value, unique);
public string get(string key) {
return (string) mmc.get(key);
public cacheitem gets(string key) {
memcacheditem item = mmc.gets(key);
return new cacheitem(key, (string) item.getvalue(), item.getcasunique());
public boolean set(string key, string value) {
return mmc.set(key, value);
public boolean delete(string key) {
return mmc.delete(key);
public boolean flushall() {
return mmc.flushall();
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIml2ZukHcvN2Xu92Yp9CXzV2Zh1WavwVbvNmLllXZ0lmL5x2bvNWYvw1LcpDc0RHaiojIsJye.gif)
* memcachedclient 单例(jdk1.5以上)
public class memcachedclientfactory extends configurableconstants{
private static volatile memcachedclient mmc;
init("memcached-client.properties");
//{ "localhost:11211", "localhost:11212", "localhost:11213" };
string[] servers = getproperty("memcached-servers","").split(",");
integer[] weights = null;
string weightscfg = getproperty("memcached-weights","");
if(weightscfg != null){
string[] wcfg = weightscfg.split(",");
weights = new integer[wcfg.length];
for (int i = 0; i < weights.length; i++) {
weights[i] = integer.valueof(wcfg[i]);
}
}else{
weights = new integer[servers.length];
weights[i] = 1;
}
pool.setinitconn(getproperty("memcached-initconn",5));
pool.setminconn(getproperty("memcached-minconn",5));
pool.setmaxconn(getproperty("memcached-maxconn",250));
private memcachedclientfactory() {
public static memcachedclient getinstance() {
if (mmc == null) {
synchronized (memcachedclient.class) {
if (mmc == null) {
mmc = new memcachedclient();
}
return mmc;
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIml2ZukHcvN2Xu92Yp9CXzV2Zh1WavwVbvNmLllXZ0lmL5x2bvNWYvw1LcpDc0RHaiojIsJye.gif)
* 通过 properties 文件配置设置常量基类 负责加载和读取 properties 属性文件并提供访问的静态工具方法
*
* @author zhangpu
*/
public class configurableconstants {
protected static log logger = logfactory.getlog(configurableconstants.class);
protected static properties p = new properties();
protected static void init(string propertyfilename) {
inputstream in = null;
try {
in = configurableconstants.class.getclassloader().getresourceasstream(propertyfilename);
if (in != null)
p.load(in);
} catch (ioexception e) {
logger.error("load " + propertyfilename + " into constants error!");
} finally {
if (in != null) {
try {
in.close();
} catch (ioexception e) {
logger.error("close " + propertyfilename + " error!");
}
}
}
}
protected static string getproperty(string key, string defaultvalue) {
return p.getproperty(key, defaultvalue);
protected static int getproperty(string key, int defaultvalue) {
return integer.parseint(getproperty(key, ""));
} catch (exception e) {
return defaultvalue;
memcached-client.properties
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIml2ZukHcvN2Xu92Yp9CXzV2Zh1WavwVbvNmLllXZ0lmL5x2bvNWYvw1LcpDc0RHaiojIsJye.gif)
memcached-client.properties
memcached-servers=localhost:11211,localhost:11212,localhost:11213
memcached-weights=3,3,2
memcached-initconn=5
memcached-minconn=5
memcached-maxconn=250
后续提供性能测试,spring整合,版本差异测试,及其它客户端对比。