Import Java.util.Date;
Import Java.util.Map;
Import com.danga.MemCached.MemCachedClient;
Import Com.danga.MemCached.SockIOPool;
public class Memcachedutil {
Create a unique instance of the global
protected static Memcachedclient MCC = new Memcachedclient ();
protected static Memcachedutil memCached = new Memcachedutil ();
Set up a connection pool to the cache server
static {
Server list and its weights
String[] Servers = {Constants.OTHERURL_PROPERTIES.getValue ("Memcached_servers")};
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 the sleep time of the main thread
Pool.setmaintsleep (10);
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 Memcachedutil () {
}
/**
* Get unique instances.
*
* @return
*/
public static Memcachedutil getinstance () {
return memCached;
}
/**
* 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);
}
/**
* Replace content with key
*
* @param key
* @param value
* @return
*/
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);
}
/**
* Delete cache based on key
*
* @param key
* @return
*/
public boolean remove (String key) {
return Mcc.delete (key);
}
/**
* Gets the object based on the specified keyword.
*
* @param key
* @return
*/
Public Object get (String key) {
return Mcc.get (key);
}
Public map<string, Object> getList (string[] keys) {
return Mcc.getmulti (keys);
}
public static void Main (string[] args) {
Memcachedutil cache = Memcachedutil.getinstance ();
Long Startdate=system.currenttimemillis ();
for (int i = 0; i <; i++) {
Cache.Add ("Test" +i, "China" +i);
// }
Long Enddate=system.currenttimemillis ();
for (int i = 0; i <; i++) {
System.out.println ("Cache key is test" + i + "value is"
+ cache.get ("test" + i));
}
System.out.print ("Get Value:" + cache.get ("test"));
}
}
Java version of Memcache static