在Java中使用Memcached快取技術-win7-64位下
阿新 • • 發佈:2019-01-25
在Java中使用Memcached快取技術。至於Memcached是什麼,原理等可以自行百度。這裡只給出在win7 64位怎麼安裝Memcached的服務和在Java 中使用Memcached的例子。
參考文件連結:原作者
需要安裝的軟體和jar包下載連結如下:
1.安裝Memcached
1 解壓縮檔案到c:\memcached
2 命令列輸入 'c:\memcached\memcached.exe -d install'
3 命令列輸入 'c:\memcached\memcached.exe -d start' ,該命令啟動 Memcached ,預設監聽埠為 11211
安裝完成後可以在服務中看到服務啟動,如下圖:
2.下載jar包匯入到工程目錄下
3.測試程式碼
package test; import com.danga.MemCached.MemCachedClient; import com.danga.MemCached.SockIOPool; /** * @author chenglitao * @version 建立時間:2015年9月16日 上午9:49:20 * @Description: */ public class MemcachedTest { public static void main(String[] args) { /** * 初始化SockIOPool,管理Memcached的連線池 */ String[] server = { "127.0.0.1:11211" }; SockIOPool pool = SockIOPool.getInstance(); pool.setServers(server); pool.setFailover(true); pool.setInitConn(10); pool.setMinConn(5); pool.setMaxConn(250); pool.setMaintSleep(30); pool.setNagle(false); pool.setSocketTO(3000); pool.setAliveCheck(true); pool.initialize(); /** * 建立Memcached例項 * */ MemCachedClient client = new MemCachedClient(); for (int i = 0; i < 1000; i++) { // 將物件加入到Memcached快取 boolean success = client.set("" + i, "Hello!"); // 從Memcached中根據Key取出資料 String result = (String) client.get("" + i); System.out.println(String.format("set( %d ): %s", i, success)); System.out.println(String.format("get( %d ): %s", i, result)); } } }
執行結果: