Renchunbing / xmemcached

Automatically exported from code.google.com/p/xmemcached
Apache License 2.0
1 stars 0 forks source link

我怎么样利用getKeyIterator来去掉缓存里面的DB #148

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
我做了一个缓存集群,但是现在有一个问题就是我怎么样去��
�态的删除我想要删除的一些数据,比如说以XXX打头的缓存数�
��我都需要清除掉;试了一下以下代码,但并不太好使!还请
帮忙看看;
代码如下:
MemcachedClientBuilder builder = new XMemcachedClientBuilder(AddrUtil
                .getAddresses("192.168.0.85:11222"));
        MemcachedClient memcachedClient = null;
        try {
            //192.168.0.85:11212 192.168.0.85:11222
            memcachedClient = builder.build();

            memcachedClient.set("hello", 0, "Hello,xmemcached");
            String value = memcachedClient.get("hello");
            System.out.println("hello=" + value);
            // memcachedClient.delete("hello");
            value = memcachedClient.get("hello");
            System.out.println("hello=" + value);

            System.out.println(memcachedClient.delete("T_MODEL_INFO_initAuditFineSelectedSQL"));

            System.out.println(memcachedClient.get("T_MODEL_INFO_initAuditFineSelectedSQL"));

             KeyIterator it = memcachedClient.getKeyIterator(new
             InetSocketAddress("192.168.0.85",11222));
             while(it.hasNext()){
                 String key = it.next();
                 System.out.println(memcachedClient.delete(key));
             }

            //          
            //          
            // System.out.println("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
            // it = memcachedClient.getKeyIterator(new
            // InetSocketAddress("192.168.0.85",11222));
            // while(it.hasNext()){
            // System.out.println(it.next());
            // }
            //          
            //          
            memcachedClient.shutdown();
        } 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
            e.printStackTrace();
        }
        System.out.println("The End!");

Original issue reported on code.google.com by zengjian...@gmail.com on 31 Aug 2011 at 7:10

GoogleCodeExporter commented 9 years ago
你这个需求在memcached里是没办法直接实现的,折衷的办法是��
�个单独的key,保存所有要删除的key集合。

KeyIterator不可靠,memcached新版本也不支持,已经deprecated,不��
�议使用。

Original comment by killme2...@gmail.com on 31 Aug 2011 at 9:54