您的位置:首页 > 数据库 > Memcache

memcached client -- memcached client for java使用

2009-11-29 00:25 603 查看
memcached client for java是另一个memcached的java客户端

http://www.whalin.com/memcached/#download

代码:

(1)MemcachedServer -- memcached的服务器

public class MemcachedServer {

private String address;
private int port;
private int weight;

public MemcachedServer(String address, int port, int weight) {
this.address = address;
this.port = port;
this.weight = weight;
}

public String getAddress() {
return address;
}

public int getPort() {
return port;
}

public int getWeight() {
return weight;
}

public String toString() {
return address + ":" + port + "," + weight;
}

}


(2)MemcachedException

@SuppressWarnings("serial")
public class MemcachedException extends Exception {

public MemcachedException() {
super();
}

public MemcachedException(Throwable t) {
super(t);
}

public MemcachedException(String error) {
super(error);
}

public MemcachedException(String error, Throwable t) {
super(error, t);
}

}


(3)PoolDefaultProperties -- memcached池初始化参数

import java.util.Properties;

public class PoolDefaultProperties extends Properties {

private static final long serialVersionUID = -7630655479181446040L;

public PoolDefaultProperties() {
super();
initDefault();
}

private void initDefault() {
initConn();
initMainSleep();
initTCP();
initFailover();
initAliveCheck();
}

protected void initConn() {
setProperty("initConn", "10");
setProperty("minConn", "10");
setProperty("maxConn", "20");
setProperty("maxIdle", String.valueOf(1000 * 60 * 30));
}

protected void initMainSleep() {
setProperty("maintSleep", String.valueOf(1000 * 5));
}

protected void initTCP() {
setProperty("nagle", "false");
setProperty("socketTO", String.valueOf(1000 * 3));
setProperty("socketConnectTO", String.valueOf(1000 * 3));
}

protected void initFailover() {
setProperty("failover", "true");
setProperty("failback", "true");
}

protected void initAliveCheck() {
setProperty("aliveCheck", "true");
}

}


(4)MemcachedPool -- memcached池

import java.lang.reflect.InvocationTargetException;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.Set;

import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.danga.MemCached.SockIOPool;

public class MemcachedPool {

private static final Log logger = LogFactory.getLog(MemcachedPool.class);

private static Properties POOL_DEFAULT_VALUE = new PoolDefaultProperties();

private static MemcachedPool pool = new MemcachedPool();

private MemcachedPool() {}

public static MemcachedPool getInstance() {
return pool;
}

public void initPool(List<MemcachedServer> servers) throws MemcachedException {
initPool(servers, POOL_DEFAULT_VALUE);
}

public void initPool(List<MemcachedServer> servers, Properties props) throws MemcachedException {
SockIOPool sockIOPool = SockIOPool.getInstance();

//server & weight
sockIOPool.setServers(getServer(servers));
sockIOPool.setWeights(getWeight(servers));

//bean props
Set keys = props.keySet();
Iterator keyIter = keys.iterator();
while (keyIter.hasNext()) {
String key = (String)keyIter.next();
String value = props.getProperty(key);
if (value == null) {
value = POOL_DEFAULT_VALUE.getProperty(key);
}
try {
Class type = PropertyUtils.getPropertyType(sockIOPool, key);
logger.debug("Type=" + type + ";Key=" + key + ";Value=" + value);
Object val = ConvertUtils.convert(value, type);
PropertyUtils.setSimpleProperty(sockIOPool, key, val);
} catch (IllegalAccessException e) {
throw new MemcachedException("Init Pool Fail", e);
} catch (InvocationTargetException e) {
throw new MemcachedException("Init Pool Fail", e);
} catch (NoSuchMethodException e) {
throw new MemcachedException("Init Pool Fail", e);
}
}
sockIOPool.initialize();
}

private Integer[] getWeight(List<MemcachedServer> weigths) {
Integer[] w = new Integer[weigths.size()];
for (int i = 0; i < weigths.size(); i++) {
w[i] = weigths.get(i).getWeight();
}
return w;
}

private String[] getServer(List<MemcachedServer> servers) {
String[] s = new String[servers.size()];
for (int i = 0; i < servers.size(); i++) {
MemcachedServer server = servers.get(i);
s[i] = server.getAddress() + ":" + server.getPort();
}
return s;
}

}


(5)MemcachedCli -- memcached操作客户端(只有set,get方法)

import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import com.danga.MemCached.MemCachedClient;

public class MemcachedCli {

private static MemcachedCli unique = new MemcachedCli();

private MemcachedCli() {
init();
}

public static MemcachedCli getInstance() {
return unique;
}

private MemCachedClient client = new MemCachedClient();

private void init() {
client.setPrimitiveAsString(true);
client.setCompressEnable(true);
client.setCompressThreshold(4 * 1024);
}

public boolean set(String key, Object value) {
return client.set(key, value);
}

public boolean set(String key, Object value, Date expired) {
return client.set(key, value, expired);
}

public Object get(String key) {
return client.get(key);
}

public void printStat() {
Map stats = client.stats();
Set keys = stats.keySet();
Iterator keyIter = keys.iterator();
while (keyIter.hasNext()) {
String key = (String)keyIter.next();
Object value = stats.get(key);
System.out.println(key + "=" + value);
}
}

}


(6)MCTest -- 简单测试

import java.util.ArrayList;
import java.util.List;

public class MCTest {

public static void main(String[] args) {
try {
MemcachedServer server = new MemcachedServer("localhost", 11211, 1);
List<MemcachedServer> servers = new ArrayList<MemcachedServer>();
servers.add(server);
MemcachedPool pool = MemcachedPool.getInstance();
pool.initPool(servers);
MemcachedCli client = MemcachedCli.getInstance();
String value = (String)client.get("test1");
System.out.println("value=" + value);
client.set("test1", "value1");
value = (String)client.get("test1");
System.out.println("value=" + value);
client.printStat();
} catch (MemcachedException e) {
e.printStackTrace();
}
}

}


测试运行结果,其中有memcached client包的调试信息:

com.danga.MemCached.MemCachedClient Sun Nov 29 00:23:54 CST 2009 - ++++ retrieving object and stuffing into a string.
value=value1
com.danga.MemCached.MemCachedClient Sun Nov 29 00:23:54 CST 2009 - ++++ storing data as a string for key: test1 for class: java.lang.String
com.danga.MemCached.MemCachedClient Sun Nov 29 00:23:54 CST 2009 - ++++ memcache cmd (result code): set test1 0 0 6
(STORED)
com.danga.MemCached.MemCachedClient Sun Nov 29 00:23:54 CST 2009 - ++++ data successfully stored for key: test1
com.danga.MemCached.MemCachedClient Sun Nov 29 00:23:54 CST 2009 - ++++ retrieving object and stuffing into a string.
value=value1
localhost:11211={bytes_written=587, connection_structures=11, bytes=52, total_items=2, total_connections=21, uptime=284045336, pid=1416, get_hits=3, curr_items=1, version=1.2.1, cmd_get=4, time=1259425433, pointer_size=32, cmd_set=2, limit_maxbytes=67108864, bytes_read=162, curr_connections=10, get_misses=1}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: