XMemcached缓存系统

XMemcached 学习整理
1、首先要把server 启动:对应到相应的目录下memcached.exe –start
2、依赖slf4j.jar包,下载slf4j 把D:\library\slf4j-1.5.5\slf4j-simple-1.5.5.jar
和D:\library\slf4j-1.5.5\slf4j-simple-1.5.5.jar放在classpath路径下
3、Memcache第一次会把数据放在内存当中,每次执行的时候如果数据没有变就不更新,直接从内存当中取。
4、如果server停掉,存在内存中的数据会消失
5、替换键对应的数值
   Replace(“key”,”value”);
Java代码  复制代码  收藏代码
  1. MemcachedClientBuilder builder = newXMemcachedClientBuilder(AddrUtil.getAddresses("10.180.44.224:11211 zhouxq:11211"),new int[]{1,3}); 

MemcachedClientBuilder 5个构造方法
构造方法
XMemcachedClientBuilder(List/Map address,int[] weight)
Address 可以是:
Java代码  复制代码  收藏代码
  1. java.util.Map<java.net.InetSocketAddress,java.net.InetSocketAddress> addressMap 
  2. java.util.List<java.net.InetSocketAddress> addressList  
weight 与servers对应的节点的权重
weight 可以有也可无
weight 值大则权重大,否则小

XMemcached允许通过设置节点的权重来调节memcached的负载,设置的权重越高,该memcached节点存储的数据将越多,所承受的负载越大。
xmemcached的权重是通过复制连接的多个引用来实现的,比如权重为3,那么就复制3个同一个连接的引用放在集合中让MemcachedSessionLocator查找。

改变节点权重,可以通过setServerWeight方法:
Java代码  复制代码  收藏代码
  1. public void setServerWeight(String server, int weight); 
传入一个int数组,里面的元素就是节点对应的权重值,比如这里设置"10.180.44.224:1121"节点的权重为1,而"zhouxq:11211"的权重为3。
类似的XMemcachedClient()和XMemcachedClientBuilder相同

设置连接池大小
Java代码  复制代码  收藏代码
  1. builder.setConnectionPoolSize(5); 
连接池通常不建议设置太大,我推荐在0-30之间为好,太大则浪费系统资源,太小无法达到分担负载的目的。
设置失败模式 如果服务出现down的情况,会报出异常,直到服务正常
Java代码  复制代码  收藏代码
  1. builder.setFailureMode(true); 
使用二进制文件
Java代码  复制代码  收藏代码
  1. builder.setCommandFactory(new BinaryCommandFactory());  
7、
存储数据是通过set方法,它有三个参数,第一个是存储的key名称,第二个是expire时间 (单位秒) ,超过这个时间,memcached将这个数据替换出去,0表示永久存储(默认是一个月),第三个参数就是实际存储的数据,可以是任意的java可序列化类型。
Java代码  复制代码  收藏代码
  1. Thread  t = new Thread(); 
  2.          t.start(); 
  3.          t.sleep(3000); 
  4.     //从memcached获取key对应的value,操作超时3秒 
[java]  view plain copy

someObject=memcachedClient.get("key",3000);后面的对应的是 毫秒
超时后得到的key对应的值为null
8、与Spring框架集成
通过XMemcachedClientFactoryBean类,即可与spring框架集成

测试代码
Java代码  复制代码  收藏代码
  1. package com.test.xmemcache; 
  2.  
  3. import java.io.IOException; 
  4. import java.util.concurrent.TimeoutException; 
  5.  
  6. import net.rubyeye.xmemcached.MemcachedClient; 
  7. import net.rubyeye.xmemcached.MemcachedClientBuilder; 
  8. import net.rubyeye.xmemcached.XMemcachedClientBuilder; 
  9. import net.rubyeye.xmemcached.exception.MemcachedException; 
  10. import net.rubyeye.xmemcached.utils.AddrUtil; 
  11.  
  12. public class XmemcacheTest { 
  13.  
  14.     /**
  15.      * @param args
  16.      */ 
  17.     public static void main(String[] args) { 
  18.         // TODO Auto-generated method stub 
  19.         MemcachedClientBuilder builder = newXMemcachedClientBuilder(AddrUtil.getAddresses("10.180.44.224:11211 zhouxq:11211"),new int[]{1,3}); 
  20.         try { 
  21.             MemcachedClient memcachedClient = builder.build(); 
  22.             memcachedClient.set("hello"1"Hello,xmemcached"); 
  23.             String Value=memcachedClient.get("hello",3000); // 
  24.             System.out.println("hello=" + Value); 
  25. //          memcachedClient.flushAll(); 
  26.             memcachedClient.replace("hello",1"zhouxq"); 
  27.             System.out.println(memcachedClient.get("hello")); 
  28. //          memcachedClient.deleteWithNoReply("hello"); //删除 
  29.             System.out.println(memcachedClient.get("hello")); 
  30. //          memcachedClient.delete("hello"); //删除 
  31. //          Value = memcachedClient.get("hello"); 
  32. //          System.out.println("hello=" + Value); 
  33.              
  34.             memcachedClient.set("key",2,"someObject"); 
  35.             //从memcached获取key对应的value 
  36.             Object someObject=memcachedClient.get("key"); 
  37.              
  38.              Thread  t = new Thread(); 
  39.              t.start(); 
  40.              t.sleep(1000); 
  41.             //从memcached获取key对应的value,操作超时3秒 
  42.             someObject=memcachedClient.get("key",1000); 
  43.             System.out.println(someObject); 
  44.              
  45.              
  46.             memcachedClient.shutdown(); 
  47.              
  48.         } catch (MemcachedException ex) { 
  49.             System.err.println("MemcachedClient operation fail"); 
  50.             ex.printStackTrace(); 
  51.         } catch (TimeoutException xe) { 
  52.             System.err.println("MemcachedClient operation timeout"); 
  53.             xe.printStackTrace(); 
  54.         } catch (InterruptedException e) { 
  55.             // ignore 
  56.         } catch (IOException e) { 
  57.             // TODO Auto-generated catch block 
  58.             e.printStackTrace(); 
  59.         } 
  60.     } 
  61.  


总结

Memcached 是一个高性能的分布式内存对象的key-value缓存系统,用于动态Web应用以减轻数据库负载,现在也有很多人将它作为内存式数据库在使用,memcached通过它的自定义协议与客户端交互,而XMemcached就是它的一个java客户端实现。

 

XMemcached使用示例(本示例基于xmemcached-1.3.8.jar),总结一个,如下:



package com.wujintao.memcached;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

import net.rubyeye.xmemcached.Counter;
import net.rubyeye.xmemcached.GetsResponse;
import net.rubyeye.xmemcached.MemcachedClient;
import net.rubyeye.xmemcached.MemcachedClientBuilder;
import net.rubyeye.xmemcached.XMemcachedClientBuilder;
import net.rubyeye.xmemcached.auth.AuthInfo;
import net.rubyeye.xmemcached.command.BinaryCommandFactory;
import net.rubyeye.xmemcached.exception.MemcachedException;
import net.rubyeye.xmemcached.transcoders.StringTranscoder;
import net.rubyeye.xmemcached.utils.AddrUtil;

import org.junit.Test;

import com.wujintao.redis.util.MD5Util;

public class TestCase {
	@Test
	public void test1() throws IOException {
		MemcachedClientBuilder builder = new XMemcachedClientBuilder(
				AddrUtil.getAddresses("localhost:11211"));
		// AddrUtil.getAddresses("server1:11211 server2:11211")
               // 宕机报警  
               builder.setFailureMode(true);  
              // 使用二进制文件  
               builder.setCommandFactory(new BinaryCommandFactory());
            /** 
             * 设置连接池大小,即客户端个数 
             * In a high concurrent enviroment,you may want to pool memcached clients. 
             * But a xmemcached client has to start a reactor thread and some thread pools, 
             * if you create too many clients,the cost is very large.  
             * Xmemcached supports connection pool instreadof client pool. 
             * you can create more connections to one or more memcached servers, 
             * and these connections share the same reactor and thread pools, 
             * it will reduce the cost of system. 
             *  默认的pool size是1。设置这一数值不一定能提高性能,请依据你的项目的测试结果为准。初步的测试表明只有在大并发下才有提升。 
             *  设置连接池的一个不良后果就是,同一个memcached的连接之间的数据更新并非同步的 
             *  因此你的应用需要自己保证数据更新的原子性(采用CAS或者数据之间毫无关联)。 
             */  
                builder.setConnectionPoolSize(10);  
		MemcachedClient client = builder.build();
		try {
			/**
			 * 第一个是存储的key名称,
			 * 第二个是expire时间(单位秒),超过这个时间,memcached将这个数据替换出去,0表示永久存储(默认是一个月)
			 * 第三个参数就是实际存储的数据
			 */
			client.set("hello", 0, "Hello,xmemcached");
			String value = client.get("hello");
			System.out.println("hello=" + value);
			client.delete("hello");
			value = client.get("hello");
			System.out.println("hello=" + value);

			// value=client.get(“hello”,3000);

			/**
			 * Memcached是通过cas协议实现原子更新,所谓原子更新就是compare and set,
			 * 原理类似乐观锁,每次请求存储某个数据同时要附带一个cas值, memcached比对这个cas值与当前存储数据的cas值是否相等,
			 * 如果相等就让新的数据覆盖老的数据,如果不相等就认为更新失败, 这在并发环境下特别有用
			 */
			GetsResponse<Integer> result = client.gets("a");
			long cas = result.getCas();
			// 尝试将a的值更新为2
			if (!client.cas("a", 0, 2, cas)) {
				System.err.println("cas error");
			}
		} catch (MemcachedException e) {
			System.err.println("MemcachedClient operation fail");
			e.printStackTrace();
		} catch (TimeoutException e) {
			System.err.println("MemcachedClient operation timeout");
			e.printStackTrace();
		} catch (InterruptedException e) {
			// ignore
		}
		try {
			// close memcached client
			client.shutdown();
		} catch (IOException e) {
			System.err.println("Shutdown MemcachedClient fail");
			e.printStackTrace();
		}

	}

	@Test
	public void test2() throws TimeoutException, InterruptedException,
			MemcachedException, IOException {
		MemcachedClientBuilder builder = new XMemcachedClientBuilder(
				AddrUtil.getAddresses("localhost:11211"));
		MemcachedClient client = builder.build();
		client.flushAll();
		if (!client.set("hello", 0, "world")) {
			System.err.println("set error");
		}
		if (client.add("hello", 0, "dennis")) {
			System.err.println("Add error,key is existed");
		}
		if (!client.replace("hello", 0, "dennis")) {
			System.err.println("replace error");
		}
		client.append("hello", " good");
		client.prepend("hello", "hello ");
		String name = client.get("hello", new StringTranscoder());
		System.out.println(name);

		/**
		 * 而删除数据则是通过deleteWithNoReply方法,这个方法删除数据并且告诉memcached
		 * 不用返回应答,因此这个方法不会等待应答直接返回,特别适合于批量处理
		 */
		client.deleteWithNoReply("hello");
	}

	@Test
	public void incrDecr() throws IOException, TimeoutException,
			InterruptedException, MemcachedException {
		MemcachedClientBuilder builder = new XMemcachedClientBuilder(
				AddrUtil.getAddresses("localhost:11211"));
		MemcachedClient client = builder.build();
		/**
		 * 第一个参数指定递增的key名称, 第二个参数指定递增的幅度大小, 第三个参数指定当key不存在的情况下的初始值。
		 * 两个参数的重载方法省略了第三个参数,默认指定为0。
		 */
		assert (1 == client.incr("a", 5, 1));
		assert (6 == client.incr("a", 5));
		assert (10 == client.incr("a", 4));
		assert (9 == client.decr("a", 1));
		assert (7 == client.decr("a", 2));
	}

	@Test
	public void counter() throws Exception {
		MemcachedClientBuilder builder = new XMemcachedClientBuilder(
				AddrUtil.getAddresses("localhost:11211"));
		MemcachedClient client = builder.build();
		Counter counter = client.getCounter("counter", 0);
		counter.incrementAndGet();
		counter.decrementAndGet();
		counter.addAndGet(-10);
	}

	public void auth() throws Exception {
		MemcachedClientBuilder builder = new XMemcachedClientBuilder(
				AddrUtil.getAddresses("localhost:11211"));
		builder.addAuthInfo(AddrUtil.getOneAddress("localhost:11211"),
				AuthInfo.typical("cacheuser", "123456"));
		// Must use binary protocol
		builder.setCommandFactory(new BinaryCommandFactory());
		MemcachedClient client = builder.build();
	}

	public void nioPool() throws Exception {
		MemcachedClientBuilder builder = new XMemcachedClientBuilder(
				AddrUtil.getAddresses("localhost:11211"));
		builder.setConnectionPoolSize(5);
	}

	
	/**
     *这里应该安装kestrel消息服务器,才能使用如下API生效
	 * @throws IOException
	 * @throws MemcachedException 
	 * @throws InterruptedException 
	 * @throws TimeoutException 
	 */
	@Test
	public void testGet() throws IOException, TimeoutException, InterruptedException, MemcachedException{
		MemcachedClientBuilder builder = new XMemcachedClientBuilder(
				AddrUtil.getAddresses("localhost:11212"));
		MemcachedClient client = builder.build();
		String value = client.get("1");
		System.out.println("hello=" + value);
	}
	
	
	@Test
    public void testGet2() throws IOException, TimeoutException, InterruptedException, MemcachedException{
        MemcachedClientBuilder builder = new XMemcachedClientBuilder(
                AddrUtil.getAddresses("localhost:11212"));
        MemcachedClient client = builder.build();
        String value = client.get("srp_"+MD5Util.MD5("3rdsearch_周杰伦"));
        System.out.println(value);
    }
}


  • 0
    点赞
  • 0
    评论
  • 1
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值