Package Com.ibs.auth.controller;
Import java.io.UnsupportedEncodingException;
Import Java.util.Date;
Import Java.util.HashMap;
Import Java.util.Iterator;
Import Java.util.Map;
Import com.whalin.memcached.MemCachedClient;
Import Com.whalin.memcached.SockIOPool;
public class Memcachedmanager {
Create a unique instance of the global
protected static Memcachedclient MCC = new Memcachedclient ();
protected static Memcachedmanager Memcachedmanager = new Memcachedmanager ();
Set up a connection pool to the cache server
static {
Server list and its weights
String[] Servers = {"127.0.0.1:11211"};
Integer[] weights = {3};
Gets the instance object of the Socke connection pool
Sockiopool pool = sockiopool.getinstance ();
Setting Up Server information
Pool.setservers (servers);
Pool.setweights (weights);
Set the number of initial connections, minimum and maximum connections, and maximum processing time
Pool.setinitconn (5);
Pool.setminconn (5);
Pool.setmaxconn (250);
Pool.setmaxidle (1000 * 60 * 60 * 6);
Set main thread sleep time, wake up every 3 seconds, maintain connection pool size
Maintsleep do not set to 30, access to a large number of problems, the unit is milliseconds, recommended 30000 milliseconds.
Pool.setmaintsleep (30);
Setting TCP parameters, connection timeouts, etc.
Pool.setnagle (FALSE);
Pool.setsocketto (3000);
Pool.setsocketconnectto (0);
Initializing the connection pool
Pool.initialize ();
Compression setting, data that exceeds the specified size (in K) is compressed
Mcc.setcompressenable (TRUE);
Mcc.setcompressthreshold (64 * 1024);
}
/**
* Protected construction method, not allowed to instantiate!
*
*/
Protected Memcachedmanager () {
}
/**
* Get unique instances.
*
* @return
*/
public static Memcachedmanager getinstance () {
return memcachedmanager;
}
/**
* Add a specified value to the cache.
*
* @param key
* @param value
* @return
*/
Public boolean Add (String key, Object value) {
Return Mcc.add (key, value);
}
Public boolean Add (String key, Object value, Date expiry) {
Return Mcc.add (key, value, expiry);
}
public boolean replace (String key, Object value) {
Return Mcc.replace (key, value);
}
public boolean replace (String key, Object value, Date expiry) {
Return Mcc.replace (key, value, expiry);
}
public Boolean set (String Key,object value) {
return Mcc.set (key, value);
}
/**
* Gets the object based on the specified keyword.
*
* @param key
* @return
*/
Public Object get (String key) {
return Mcc.get (key);
}
/**
* Delete key
*/
public boolean Delete (String key) {
return Mcc.delete (key);
}
public static map<string,object> Getkeysformap () throws unsupportedencodingexception{
Map<string,object> keylist=new hashmap<string,object> ();
Traverse Statsitems Get Items: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 get the key in each item
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 (); Iterator_keys.hasnext ();) {
statscachedumpsub_key= (String) iterator_keys.next ();
Statscachedumpsub_key_value=statscachedump_sub.get (Statscachedumpsub_key);
System.out.println (Statscachedumpsub_key);//key is encoded in Chinese, the client is encoded before set, and the server-side Chinese key is ciphertext
System.out.println (Statscachedumpsub_key_value);
Keylist.put (Statscachedumpsub_key,statscachedumpsub_key_value);
} }
}
}
}
return keylist;
}
public static void Main (string[] args) {
Memcachedmanager cache = Memcachedmanager.getinstance ();
try {
Map<string,object> Map=getkeysformap ();
for (int i=0;i<map.size (); i++) {
if (Map.containskey ("1")) {
System.out.println ("It ' s OK");
}
}
} catch (Unsupportedencodingexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
Cache.Add ("Hello", 234);
System.out.print ("Get Value:" + cache.get ("Hello"));
}
}
This article is from the "Jia Xiaoxian" blog, make sure to keep this source http://hackerxian.blog.51cto.com/9240575/1607010
Memcached's tool class. Get all keys in the cached