首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > 其他数据库 >

XMemcached应用示例

2013-04-05 
XMemcached使用示例package com.wujintao.memcachedimport java.io.IOExceptionimport java.util.concur

XMemcached使用示例
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 {@Testpublic void test1() throws IOException {MemcachedClientBuilder builder = new XMemcachedClientBuilder(AddrUtil.getAddresses("localhost:11211"));// AddrUtil.getAddresses("server1:11211 server2:11211")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的值更新为2if (!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 clientclient.shutdown();} catch (IOException e) {System.err.println("Shutdown MemcachedClient fail");e.printStackTrace();}}@Testpublic 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");}@Testpublic 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));}@Testpublic 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 protocolbuilder.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 */@Testpublic 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); }}

?

?

热点排行