生活随笔
收集整理的這篇文章主要介紹了
Memcache配置
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
簡述:
配置memcache服務
步驟:
1. 下載memcached
c源文件(需要編譯)
http://code.google.com/p/memcached/downloads/list
windows binary 文件(windows上運行)
http://code.jellycan.com/memcached/
2. 解壓到文件夾memcached
3. 用管理員打開cmd, cd進入該文件夾,
首先安裝該服務
[html]?view plaincopy
memcached.exe?-d?install??
開啟memcache服務
[plain]?view plaincopy
memcached.exe?-d?start??
3.使用netstat -a 可以看到memcached服務端口11211已經打開
4. Maven導入需要的memcached客戶端包
包括(slf4j-log4j), 用來日志輸出的
[html]?view plaincopy
<dependency>?? ????<groupId>com.whalin</groupId>?? ????<artifactId>Memcached-Java-Client</artifactId>?? ????<version>3.0.1</version>?? </dependency>?? <dependency>?? ????<groupId>org.slf4j</groupId>?? ????<artifactId>slf4j-log4j12</artifactId>?? ????<version>1.7.2</version>?? </dependency>??
5.進入eclipse進行測試(轉自:?http://jason61719.iteye.com/blog/1002141)
[java]?view plaincopy
package?com.anialy.project.springmvc.helper;?? ?? import?com.whalin.MemCached.MemCachedClient;?? import?com.whalin.MemCached.SockIOPool;?? ?? public?class?CacheHelper?{?? ?????? ?????? ????protected?static?MemCachedClient?mcc?=?new?MemCachedClient();?? ?????? ????private?CacheHelper()?{?? ????}?? ?????? ?????? ????static?{?? ?????????? ????????String[]?servers?=?{?"127.0.0.1:11211"?};?? ?????????? ?????????? ????????Integer[]?weights?=?{?2?};?? ?????????? ?????????? ????????SockIOPool?pool?=?SockIOPool.getInstance();?? ?????????? ?????????? ????????pool.setServers(servers);?? ????????pool.setWeights(weights);?? ?????????? ?????????? ????????pool.setInitConn(5);?? ????????pool.setMinConn(5);?? ????????pool.setMaxConn(250);?? ????????pool.setMaxIdle(1000?*?60?*?60?*?6);?? ?????????? ?????????? ????????pool.setMaintSleep(30);?? ?????????? ?????????? ????????pool.setNagle(false);?? ????????pool.setSocketConnectTO(0);?? ????????pool.setSocketTO(3000);?? ?????????? ?????????? ????????pool.initialize();?? ????}?? ?????? ????public?static?boolean?set(String?arg0,?Object?arg1)?{?? ????????return?mcc.set(arg0,?arg1);?? ????}?? ?????? ????public?static?Object?get(String?arg0)?{?? ????????return?mcc.get(arg0);?? ????}?? ?????? ????public?static?void?main(String[]?args)?{?? ????????CacheHelper.set("1",?"cache_1");?? ????????System.out.println(CacheHelper.get("1"));?? ????????System.out.println(CacheHelper.get("2"));?? ????}?? }??
輸出:
總結
以上是生活随笔為你收集整理的Memcache配置的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。