Java MemCachedClient traverses all keys in memcache

Source: Internet
Author: User

Java memcached client documentation does not provide a method to traverse all memcache keys. However, two methods are provided: statsItems and statsCacheDump. statsitems can be used to obtain the number of items in memcache and the number of keys on each item, statsCacheDump can obtain information about each key (key name, size, and validity period) on each item ). The following is the address of its api documentation: Through this example, I have such a problem. A value-key-Value Pair stored in memcache. The value can be a basic data type or an object type. Client A stores two keys. key1 is of the String type and key2 is of the Object type. Then how does client B know that key1 is of the String type and key2 is of the Object type. (Let's take a look at the set method. The corresponding values are all objects ). To put it bluntly, how does one differentiate the Data Types of keys ????? Well, remember this first. The following is a method for getting the key. KeysBean defines a javabean with three attributes in total: This is not detailed here. Private String server; private long bytes; private long expiry;

Public static Map <String, KeysBean> getKeysForMap () throws UnsupportedEncodingException {Map <String, KeysBean> keylist = new HashMap <String, KeysBean> (); // retrieve items by traversing statsItems: 2: number = 14 Map <String, Map <String, String> statsItems = mcc. statsItems (); Map <String, String> statsItems_sub = null; String statsItems_sub_key = null; int items_number = 0; String server = null; // according to items: 2: number = 14, call statsCacheDump to obtain the key Map <String, Map <String, String> statsCacheDump = null; Map <String, String> statsCacheDump_sub = null; String statsCacheDumpsub_key = null; string statsCacheDumpsub_key_value = null; for (Iterator iterator = statsItems. keySet (). iterator (); iterator. hasNext ();) {server = (String) iterator. next (); statsItems_sub = statsItems. get (server); // System. out. println (server + "=" + statsItems_sub); for (Iterator iterator_item = statsItems_sub.keySet (). iterator (); iterator_item.hasNext ();) {statsItems_sub_key = (String) iterator_item.next (); // System. out. println (statsItems_sub_key + ": =:" + bb); // items: 2: number = 14 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 (); // System. out. println (statsItems_sub_key + ": =:" + items_number); statsCacheDump = mcc. statsCacheDump (new String [] {server}, Integer. parseInt (statsItems_sub_key.split (":") [1]. trim (), items_number); for (Iterator statsCacheDump_iterator = statsCacheDump. keySet (). iterator (); statsCacheDump_iterator.hasNext ();) {statsCacheDump_sub = statsCacheDump. get (statsCacheDump_iterator.next (); // System. out. println (statsCacheDump_sub); for (Iterator iterator_keys = statsCacheDump_sub.keySet (). iterator ();) {statsCacheDumpsub_key = (String) iterator_keys.next (); iterator = statsCacheDump_sub.get (statsCacheDumpsub_key); // System. out. println (statsCacheDumpsub_key); // The key is encoded in Chinese and is encoded by the client before set. The Chinese key of the server is stored in ciphertext // System. out. println (statsCacheDumpsub_key_value); keylist. put (URLDecoder. decode (statsCacheDumpsub_key, "UTF-8"), new KeysBean (server, Long. parseLong (statsCacheDumpsub_key_value.substring (1, statsCacheDumpsub_key_value.indexOf ("B;")-1 ). trim (), Long. parseLong (statsCacheDumpsub_key_value.substring (statsCacheDumpsub_key_value.indexOf ("B;") + 2, statsCacheDumpsub_key_value.indexOf ("s]")-1 ). trim () ;}}}} return keylist ;}

 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.