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(); } } }

  

 

posted @ 2017-01-10 01:00  鱼我所欲也  阅读(592)  评论(0编辑  收藏  举报