日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2024/9/19 编程问答 49 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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}

總結

以上是生活随笔為你收集整理的java memcachedclient_memcached client — memcached client for java使用 | 学步园的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。