rss· 投稿· 设为首页· 加入收藏· 繁體版
当前位置: 火魔网 » 程序开发 » Java综合

xmemcached使用之实例

使用实例
3.1介绍
目前支持所有的memcached 文本协议(二进协议正在开发中,预计在1.2中会出现) ,现包括了 get/gets、set、add、replace、delete、append、prepend、cas、multi get/gets、incr、decr、version、stats、flush_all等。
3.2 简单实例
MemcachedClient client;
   try {
    client = new XMemcachedClient("localhost",11211);//默认端口
    // store a value for one hour(synchronously).
    String someObject = "缓存这个一个小时可以吗?";
    client.set("key", 3600, someObject);

    // Retrieve a value.(synchronously).
    Object getSomeObject = client.get("key");
    // delete
    client.delete("key");
    System.out.println(getSomeObject.toString());
   } catch (TimeoutException e) {
    e.printStackTrace();
   } catch (InterruptedException e) {
    e.printStackTrace();
   } catch (MemcachedException e) {
    e.printStackTrace();
   } catch (IOException e) {
    e.printStackTrace(); 以上代码是没有经过Spring管理的,XMemcachedClient是一个常驻程序。所以可以考虑将它置入到Spring中,配置方式前面已讲过。 private MemcachedClient memcachedClient;

public void setMemcachedClient(MemcachedClient memcachedClient) {
   this.memcachedClient = memcachedClient; 注入后直接可使用。3.3 监听服务器映射
Xmemcached源代码中XMemcachedClientMBean的setServerWeight(String server, int weight)方法可以将被监听服务器进行编号。具体使用方式可以用:
MemcachedClientBuilder builder = new    XMemcachedClientBuilder(AddrUtil.getAddresses("localhost:12000 localhost:12001"),new int[]{1,3});   //编号为1,3
MemcachedClient memcachedClient=builder.build();
3.4动态改变被监听服务器
Xmemcached支持动态的增加或者删除监听服务器,方式如下:
MemcachedClient client=new XMemcachedClient(AddrUtil.getAddresses("server1:11211 server2:11211"));  
//Add two new memcached nodes  
client.addServer("server3:11211 server4:11211");  
//Remove memcached servers  
client.removeServer("server1:11211 server2:11211");
3.5 使用CAS更新缓存
CASOperation需要先gets获取cas值,然后再调用cas方法更新,XMemcached提供了一个包装类可以帮你搞定这两步。以下是一个使用实例:
class CASThread extends Thread {
        static final class IncrmentOperation implements CASOperation<Integer> {                  *Max repeat times.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 currentValue + 1; // 当前值+1         }

        private XMemcachedClient mc;
        private CountDownLatch cd;

        public CASThread(XMemcachedClient mc, CountDownLatch cdl) {
                super();
                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 (Exception 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(args[0]);
                XMemcachedClient mc = new XMemcachedClient(AddrUtil.getAddresses(args[1]));
                //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("test cas,timed:"
                                + (System.currentTimeMillis() - start));
                System.out.println("result=" + mc.get("a"));
                mc.shutdown(); }

顶一下
(0)
踩一下
(0)