java memcachedclient_memcached client — memcached client for java使用 | 学步园

memcached client for java是另一个memcached的java客户端

代码:

(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 servers) throws MemcachedException {

initPool(servers, POOL_DEFAULT_VALUE);

}

public void initPool(List 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 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 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 servers = new ArrayList();

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}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值