java MemCache client application Model
Posted on 2009-11-16 17:12 Gavin.lee 閱讀(679) 評論(0) 編輯 收藏 所屬分類: MemCache這段時間解決系統的瓶頸,首要是對memcache的優化,今天得空將目前在用的memcache整理了一下,貼個客戶端實體類-----這里設置的參數都是通過配置文件的。道理很簡單,以戒后用
(注:http://code.google.com/p/memcache-client-forjava/downloads/list 本次使用的是來自這里的封裝包,網上結論說alisoft memcache 性能不高,經高壓下使用,個人感覺確實不妥。
可考慮使用 http://www.whalin.com/memcached/#download )
package yixun.wap.cache;

/**
* @deprecated mem-cache緩存類 備用類
* @author Administrator
* @date 2009-11-15 14:08:02 更新
*/
import java.util.Date;
import com.alisoft.xplatform.asf.cache.memcached.client.MemCachedClient;
import com.alisoft.xplatform.asf.cache.memcached.client.SockIOPool;
import yixun.wap.resourcesload.MemcacheConfig;

public class MemCache {
private static MemCachedClient mcc = null;
static {
mcc = new MemCachedClient();
Boolean nagle = false;
Boolean ce = true;
// 獲取socke連接池的實例對象
SockIOPool pool = SockIOPool.getInstance();
// 設置服務器信息
pool.setServers(MemcacheConfig.getServers().split(";"));
String[] weights = MemcacheConfig.getWeights().split(";");
Integer[] wei = new Integer[weights.length];
for (int i = 0; i < weights.length; i++) {
wei[i] = Integer.parseInt(weights[i]);
}
pool.setWeights(wei);
// 設置初始連接數、最小和最大連接數以及最大處理時間
pool.setInitConn(Integer.parseInt(MemcacheConfig.getInitconn()));
pool.setMinConn(Integer.parseInt(MemcacheConfig.getMinconn()));
pool.setMaxConn(Integer.parseInt(MemcacheConfig.getMaxconn()));
pool.setMaxIdle(Long.parseLong(MemcacheConfig.getMaxidle()));
// 設置主線程的睡眠時間
pool.setMaintSleep(Long.parseLong(MemcacheConfig.getMaintsleep()));
if (!MemcacheConfig.getNagle().equals("false")) {
nagle = true;
}
// 設置TCP的參數,連接超時等
pool.setNagle(nagle);
pool.setSocketTO(Integer.parseInt(MemcacheConfig.getSocketTO()));
pool.setSocketConnectTO(Integer.parseInt(MemcacheConfig.getSocketConnectTO()));
// 初始化連接池
pool.initialize();
// 壓縮設置,超過指定大小(單位為K)的數據都會被壓縮
if (MemcacheConfig.getCompressEnable().equals("false")) {
ce = false;
}
mcc.setCompressEnable(ce);
mcc.setCompressThreshold(Long.parseLong(MemcacheConfig.getCompressThreshold()));
}
private MemCache(){
}
private static MemCache instance;
public synchronized static MemCache getInstance() {
if(instance == null) {
return new MemCache();
}
return instance;
}
// 在memcache中對數據進行存、取、刪、更新
public boolean set(String key, Object value) {
return mcc.set(key, value);
}

public boolean set(String key, Object value, String time) {
return mcc.set(key, value, new Date(Long.parseLong(time)));
}

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

public boolean delete(String key) {
return mcc.delete(key);
}
public boolean update(String key, Object obj) {
return mcc.replace(key, obj);
}
public boolean update(String key, Object value, Date expiry) {
return mcc.replace(key, value, expiry);
}
public void flush_all() {
mcc.flushAll();
}


public MemCachedClient getMcc() {
return mcc;
}


public void setMcc(MemCachedClient mcc) {
this.mcc = mcc;
}

}
(注:http://code.google.com/p/memcache-client-forjava/downloads/list 本次使用的是來自這里的封裝包,網上結論說alisoft memcache 性能不高,經高壓下使用,個人感覺確實不妥。
可考慮使用 http://www.whalin.com/memcached/#download )
















































































































