所須要的jar包:
com.danga.MemCached.MemCachedClient
com.danga.MemCached.SockIOPool 自行下載
/** * 緩存服務器集羣,提供緩存鏈接初始化,獲取緩存客戶端等工做 * @author ray */ public class CacheCluster { private final static MemCachedClient client = new MemCachedClient(); private static String _memcache_config = "/WEB-INF/config/memcached.properties"; static { Properties config = FileUtil.getProperties(HttpContext.getPathPrefix() + _memcache_config); String serverGroup = config.getProperty("server"); String[] servers = serverGroup.split(";"); SockIOPool pool = SockIOPool.getInstance(); int init_conns = Integer.valueOf(config.getProperty("conn_init")); int min_spare = Integer.valueOf(config.getProperty("conn_minspare")); int max_spare = Integer.valueOf(config.getProperty("conn_maxspare")); long idel_time = Long.valueOf(config.getProperty("conn_maxideltime")); long busy_time = Long.valueOf(config.getProperty("conn_maxbusytime")); int timeout = Integer.valueOf(config.getProperty("conn_timeout")); pool.setServers(servers); pool.setInitConn(init_conns); pool.setMinConn(min_spare); pool.setMaxConn(max_spare); pool.setMaxIdle(idel_time); pool.setMaxBusyTime(busy_time); pool.setSocketTO(timeout); pool.setFailover(true); pool.initialize(); client.setCompressEnable(true); client.setCompressThreshold(64 * 1024); } public static MemCachedClient getCacheClient() { return client; } }
properties配置文件:緩存
server=192.168.11.144:1121 conn_init=100 初始化空間大小kb conn_minspare=100 最小分配空間kb conn_maxspare=1000 cache空間kb conn_maxideltime=1800000 conn_maxbusytime=300000 最長鏈接數量 conn_timeout=3000 鏈接最長時限