Java_memcached 使用说明
生活随笔
收集整理的這篇文章主要介紹了
Java_memcached 使用说明
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
1.加載commons-pool-1.5.6.jar、java_memcached-release_2.6.6.jar、slf4j-api-1.6.1.jar、slf4j-simple-1.6.1.jar
2.創(chuàng)建memcached工具類:
public class MemcachedUtil { /** * memcached客戶端單例 */ private static MemCachedClient cachedClient = new MemCachedClient(); /** * 初始化連接池 */ static { //獲取連接池的實(shí)例 SockIOPool pool = SockIOPool.getInstance(); //服務(wù)器列表及其權(quán)重 String[] servers = {"127.0.0.1:11211"}; Integer[] weights = {3}; //設(shè)置服務(wù)器信息 pool.setServers(servers); pool.setWeights(weights); //設(shè)置初始連接數(shù)、最小連接數(shù)、最大連接數(shù)、最大處理時(shí)間 pool.setInitConn(10); pool.setMinConn(10); pool.setMaxConn(1000); pool.setMaxIdle(1000*60*60); //設(shè)置連接池守護(hù)線程的睡眠時(shí)間 pool.setMaintSleep(60); //設(shè)置TCP參數(shù),連接超時(shí) pool.setNagle(false); pool.setSocketTO(60); pool.setSocketConnectTO(0); //初始化并啟動(dòng)連接池 pool.initialize(); //壓縮設(shè)置,超過指定大小的都?jí)嚎s // cachedClient.setCompressEnable(true); // cachedClient.setCompressThreshold(1024*1024); } private MemcachedUtil(){ } public static boolean add(String key, Object value) { return cachedClient.add(key, value); } public static boolean add(String key, Object value, Integer expire) { return cachedClient.add(key, value, expire); } public static boolean put(String key, Object value) { return cachedClient.set(key, value); } public static boolean put(String key, Object value, Integer expire) { return cachedClient.set(key, value, expire); } public static boolean replace(String key, Object value) { return cachedClient.replace(key, value); } public static boolean replace(String key, Object value, Integer expire) { return cachedClient.replace(key, value, expire); } public static Object get(String key) { return cachedClient.get(key); } }轉(zhuǎn)載于:https://my.oschina.net/rouchongzi/blog/131174
與50位技術(shù)專家面對(duì)面20年技術(shù)見證,附贈(zèng)技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的Java_memcached 使用说明的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Struts2 标签配置详细
- 下一篇: DNS之三 辅助区域和名称解析过程