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

java memcache应用

2016-04-15 17:35 471 查看
package com.pt.util.memcached;

import com.danga.MemCached.MemCachedClient;
import com.danga.MemCached.SockIOPool;

public class MemcachedTool {
private static MemCachedClient memcacheClient = null;

private MemcachedTool(){

}

public static MemCachedClient getInstance(){
if(memcacheClient == null){
memcacheClient = new MemCachedClient();
memcacheClient.setDefaultEncoding("UTF-8");    //写入缓存的编码格式

}
return memcacheClient;
}

static{
String[] serversArray = {"192.168.65.221:13220"};
Integer[] weight = {1};
SockIOPool connPool = SockIOPool.getInstance();
connPool.setServers(serversArray);        //设置memcached服务器
connPool.setWeights(weight);            //设置各个服务器存储权重
connPool.setMinConn(3);                    //设置连接池的最小连接数目
connPool.setInitConn(3);                //初始化可用连接数目
connPool.setMaxIdle(10000);                //可用连接池最长等待时间
connPool.setSocketTO(10000);            //读取等待超时值
connPool.setSocketConnectTO(10000);        //连接等待超时值
//心跳检测,设置为true时,每次通信都会先检测连接是否可用 增加IO和网络开销,一般设置为false 默认是false
connPool.setAliveCheck(false);
/**
*  alg=0 使用String.hashCode()获得hash code,该方法依赖JDK,可能和其他客户端不兼容,建议不使用
*    alg=1 使用original 兼容hash算法,兼容其他客户端
*    alg=2 使用CRC32兼容hash算法,兼容其他客户端,性能优于original算法
*    alg=3 使用MD5 hash算法
*    采用前三种hash算法的时候,查找cache服务器使用余数方法。采用最后一种hash算法查找cache服务时使用consistent方法。
**/
connPool.setHashingAlg(3);
//设置服务器宕机或连接由中断变为恢复后,该连接继续可用
connPool.setFailback(true);
//启动pool
connPool.initialize();
}
}


MemcacheTool
需要引入:java-memcached-release_2.5.2.jar包
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: