常用命令:
memcached設計的原則就是簡單,所以支持的命令也不是特別多~
1.查看memcached的狀態,主要用於分析內存的使用狀況、優化內存分配等
stats 查看memcached的運行狀態 stats items 查看items的狀態 stats slabs 查看slabs的內存分配狀態,注重點在性能而非計數 stats sizes 查看
2.存
set 存值,若key已存在會覆蓋原值 add 存值,若key已存在保存失敗 replace 替換原值,若key不存在替換失敗 append 在原value的末尾上追加內容 prepend 在原value的頭部追加內容 cas(check and set) 檢索並設置值 incr 取值自增 decr 取值自減
3.取
get key 獲取某個key的值 gets key1 key2 獲取多個key的值
4.刪
delete key 刪除某個key flush_all 清除所有存儲的內容,不會釋放內存
基於java客戶端的三種實現
1.使用最久、最廣泛的memcached client for java
package com.wang.client.danga; import com.danga.MemCached.MemCachedClient; import com.danga.MemCached.SockIOPool; /** * memcached client for java 較早推出,應用廣泛、運行穩定 * 下載地址:https://cloud.github.com/downloads/gwhalin/Memcached-Java-Client/java_memcached-release_2.6.6.zip * 解壓出依賴jar包: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 * @author wlyfree */ public class MemcachedClientForJava_Danga_Demo { public static void main(String[] args) { MemCachedClient client = new MemCachedClient(); // 初始化SockIOPool,管理memcached連接池 SockIOPool pool = SockIOPool.getInstance(); // 配置 String[] servers = new String[] { "10.90.11.142:11211", "10.90.11.142:11212", "10.90.11.142:11213" }; pool.setServers(servers); pool.setFailover(true); pool.setInitConn(10); // 設置初始連接 pool.setMinConn(5);// 設置最小連接 pool.setMaxConn(250); // 設置最大連接 pool.setMaxIdle(1000 * 60 * 60 * 3); // 設置每個連接最大空閒時間3個小時 pool.setMaintSleep(30); pool.setNagle(false); pool.setSocketTO(3000); pool.setAliveCheck(true); pool.initialize(); //測試 System.out.println(client.add("aa", "11")); System.out.println(client.get("aa")); System.out.println(client.set("aa", "22")); System.out.println(client.get("aa")); System.out.println(client.add("aa", "33")); System.out.println(client.get("aa")); System.out.println(client.delete("aa")); System.out.println(client.get("aa")); } }
2.spymemcached
package com.wang.client.spy; import java.io.IOException; import java.net.InetSocketAddress; import net.spy.memcached.MemcachedClient; import net.spy.memcached.internal.OperationFuture; /** * 性能穩定、穩定性略差 * 依賴jar包:spymemcached-2.10.2.jar * 當集群內某節點down機,數據不會hash到新節點,而是直接失敗。修改源碼可能會修復此問題:http://colobu.com/2015/11/24/One-spymemcached-issue-when-one-node-fails/ * 總結完了,感覺這麼不穩定的東西應該沒人在生產環境使用吧! * @author wlyfree */ public class SpyMemcached_Demo { public static void main(String[] args) { try { MemcachedClient client = new MemcachedClient(new InetSocketAddress("10.90.11.142", 11211)); OperationFuture<Boolean> operationFuture = client.add("spy", 0, "spy1"); System.out.println(operationFuture.getStatus() + "==========" + client.get("spy")); operationFuture = client.set("spy", 0, "spy2"); System.out.println(operationFuture.getStatus() + "==========" + client.get("spy")); operationFuture = client.add("spy", 0, "spy3"); System.out.println(operationFuture.getStatus() + "==========" + client.get("spy")); operationFuture = client.delete("spy"); System.out.println(operationFuture.getStatus() + "==========" + client.get("spy")); } catch (IOException e) { e.printStackTrace(); } } }
3.xmemcached
package com.wang.client.xmemcached; import net.rubyeye.xmemcached.MemcachedClient; import net.rubyeye.xmemcached.XMemcachedClient; /** * 基於nio實現,性能好,效率高,資源耗費少
* 依賴jar包:xmemcached-版本號.jar * 程序托管到github了:https://github.com/killme2008/xmemcached/ * 作者:原淘寶某幾位大神 * @author wlyfree */ public class xmemcached_Demo { public static void main(String[] args) { try { MemcachedClient client = new XMemcachedClient("10.90.11.142",11211); String key = "xmemcached"; System.out.println(client.add(key,0,"x1")); System.out.println(client.get(key)); System.out.println(client.set(key,0,"x2")); System.out.println(client.get(key)); System.out.println(client.add(key,0,"x3")); System.out.println(client.get(key)); System.out.println(client.delete(key)); System.out.println(client.get(key)); } catch (Exception e) { e.printStackTrace(); } } }