缓存研究之Xmemcached_第1页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、缓存研究之xmemcached用法实例3.1介绍目前支持全部的memcached 文本协议(二进协议正在开发中,预计在1.2中会浮现) ,现包括了 get/gets、set、add、replace、delete、append、prepend、cas、multi get/gets、ir、decr、version、s、flush_all等。3.2 容易实例memcachedclient client; try client = new xmemcachedclient("localhost",11211);/默认端口 / store a value for one hour(h

2、ronously). string someobject = "缓存这个一个小时可以吗?" client.set("key", 3600, someobject); / retrieve a value.(synchronously). object getsomeobject = client.get("key"); / delete client.delete("key"); system.out.print(getsomeobject.tostring(); catch (timeoutexception e

3、) e.printstacktrace(); catch (interruptedexception e) e.printstacktrace(); catch (memcachedexception e) e.printstacktrace(); catch (ioexception e) e.printstacktrace(); 以上代码是没有经过spring管理的,xmemcachedclient是一个常驻程序。所以可以考虑将它置入到spring中,配置方式前面已讲过。用法方式见下: private memcachedclient memcachedclient; public vo s

4、etmemcachedclient(memcachedclient memcachedclient) this.memcachedclient = memcachedclient;注入后挺直可用法。3.3 监听服务器映射xmemcached源代码中xmemcachedclientmbean的setserverweight(string server, int weight)办法可以将被监听服务器举行编号。详细用法方式可以用: memcachedclientbuilder builder = new xmemcachedclientbuilder(addrutil.getaddresses(&q

5、uot;localhost:12000 localhost:12001"),new int1,3); /编号为1,3memcachedclient memcachedclient=builder.build();3.4动态转变被监听服务器xmemcached支持动态的增强或者删除监听服务器,方式如下:memcachedclient client=new xmemcachedclient(addrutil.getaddresses("server1:11211 server2:11211"); /add two new memcached nodes client.

6、addserver("server3:11211 server4:11211"); /remove memcached servers client.removeserver("server1:11211 server2:11211");3.5 用法cas更新缓存casoperation需要先gets猎取cas值,然后再调用cas办法更新,xmemcached提供了一个包装类可以帮你搞定这两步。以下是一个用法实例:class casthread tends thread static final class incentoperation impleme

7、nts casoperation integer /* *max repeat s.if repeat times is great than this value, *xmemcached will throw a timeoutexception. */ override public int getmaxtries() return integer.max_value; /increase current value override public integer getnewvalue(long currentcas, integer currentvalue) return curr

8、entvalue + 1; / 当前值+1 private xmemcachedclient mc; private countdownlatch ; public casthread(xmemcachedclient mc, countdownlatch cdl) per(); this.mc = mc; this.cd = cdl; public void run() try /do the cas operation if (mc.cas("a", 0, new incrmentoperation() this.cd.countdown(); catch (excep

9、tion e) e.printstacktrace(); public class castest public static void main(string args) throws exception if (args.length 2) system.err.println("usage:java castest threadnum server"); system.exit(1); /threads num int num = integer.parseint(args0); xmemcachedclient mc = new xmemcachedclient(addrutil.getaddresses(args1); /initial value is 0 mc.set("a", 0, 0); countdownlatch cdl = new countdownlatch(num); long start = system.currenttimemillis(); /start num threads to increment the value for (int i = 0; i num; i+) new casthread(mc, cdl).start(); cdl.await(); system.out.println(&

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论