Java中memcached 获取所有key

 

转至:http://blog.csdn.net/e_wsq/article/details/10631159

参考:http://www.ttlsa.com/memcache/memcache-list-all-keys/

/**
* @Title: getAllKey
* @Description: 获取memcached中的所有key
* @return
* @return: List<String>
*/
public static List<String> getAllKey(){
  List<String> keylist=new ArrayList<String>();
  Map<String,Map<String,String>> statsItems = cachedClient.statsItems(); //获取所有items
  Map<String,String> statsItems_sub = null;
  String server = null;
  String statsItems_sub_key = null;
  int items_number=0;

  Map<String,Map<String,String>> statsCacheDump = null;
  Map<String,String> statsCacheDump_sub= null;
  String statsCacheDumpsub_key= null;
  for(Iterator<String> iterator=statsItems.keySet().iterator();iterator.hasNext();){
    server=iterator.next(); //server地址
    keylist.add(server);
    statsItems_sub=statsItems.get(server);
    for(Iterator<String> iterator_item=statsItems_sub.keySet().iterator();iterator_item.hasNext();){
      statsItems_sub_key = iterator_item.next();
      if(statsItems_sub_key.toUpperCase().startsWith("items:".toUpperCase()) && statsItems_sub_key.toUpperCase().endsWith(":number".toUpperCase())){
        items_number=Integer.parseInt(statsItems_sub.get(statsItems_sub_key).trim());
        statsCacheDump=cachedClient.statsCacheDump(new String[]{server},Integer.parseInt(statsItems_sub_key.split(":")[1].trim()), items_number); //获取一个item
          for (Iterator<String> statsCacheDump_iterator=statsCacheDump.keySet().iterator();statsCacheDump_iterator.hasNext();) {
            statsCacheDump_sub=statsCacheDump.get(statsCacheDump_iterator.next());
              for (Iterator<String> iterator_keys=statsCacheDump_sub.keySet().iterator();iterator_keys.hasNext();) {
                statsCacheDumpsub_key=(String) iterator_keys.next(); //获取item的key
                keylist.add(statsCacheDumpsub_key);
              }
          }
      }
    }
  }
  return keylist;
}

memcached  api:http://javadox.com/com.whalin/Memcached-Java-Client/3.0.0/com/whalin/MemCached/MemCachedClient.html

 

posted @ 2017-03-08 14:47  methink静静  阅读(2475)  评论(0编辑  收藏  举报