memcached學習——常用命令+基於java客戶端的3種復雜完成(二)。本站提示廣大學習愛好者:(memcached學習——常用命令+基於java客戶端的3種復雜完成(二))文章只能為提供參考,不一定能成為您想要的結果。以下是memcached學習——常用命令+基於java客戶端的3種復雜完成(二)正文
常用命令:
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(); } } }