In web development, with the increase of data volume, the system is under more and more pressure, especially multithreading and database access, all the while testing the system. For this reason, some caching tools can greatly relieve the pressure of the system.
Let's introduce the memcached cache tool.
The jar package you need to use is as follows, click on the link to download
Java_memcached-release_1.6.jar
For an installation of the memcached, see my other article
Installation of memcached under Windows
public class
Memcachedutil.java
Package com.lkx;
Import com.danga.MemCached.MemCachedClient;
Import Com.danga.MemCached.SockIOPool; /**** * Memcachedutil.java v1.0 February 23, 2016 15:51:53 * * @author element Sword Step Green Dust * change history: (Modify person, modify time, change reason/content) * Function Description: Memcached Tool class/public class Memcachedutil {/** * memcached client single case, unique instance/private static Memcachedclien
T cachedclient = new memcachedclient ();
/** * Initialize connection pool/static {//Get instance of connection pool Sockiopool pool = sockiopool.getinstance ();
Server list and its weight string[] servers = {"127.0.0.1:11211"};
Integer[] weights = {3};
Set up server information pool.setservers (servers);
Pool.setweights (weights);
Set the initial number of connections, the minimum number of connections, the maximum number of connections, the maximum processing time pool.setinitconn (10);
Pool.setminconn (10);
Pool.setmaxconn (1000);
Pool.setmaxidle (1000 * 60 * 60);
Sets the sleep time of the connection pool daemon Pool.setmaintsleep (60);
Set TCP parameters, connection timeout pool.setnagle (false);
Pool.setsocketto (60);
Pool.setsocketconnectto (0);
Initialize and start the connection pool pool.initialize (); Compression settings, compressed//cachedclient.se over a specified sizeTcompressenable (TRUE);
Cachedclient.setcompressthreshold (1024*1024);
Cachedclient.setprimitiveasstring (TRUE);//Set Serialization}/** * Constructor: Tool class, prohibit instantiation/private Memcachedutil () {}/*** * Function Description: Add a cache data, if key exists will not add * * @param cached key * @param cached value * @return operation result/public static Boolean add (String
Key, Object value) {return Cachedclient.add (key, value); /** * Feature Description: Add a cache data, set the expiration time parameter is seconds * @param cache key * @param cached value * @param cache time * @return operation result/public Stati
C Boolean Add (String key, Object value, Integer expire) {return Cachedclient.add (key, value, expire); /** * Feature Description: Add a cache data, if there is a key, then update the value of the key * @param cached Key * @param cached value * @return operation result/public static Boolean
Put (String key, Object value) {return Cachedclient.set (key, value); /** * Feature Description: Add a cache data, if there is a key, then update the value of the key * @param cached Key * @param cached value * @param cache time * @return Operation result * * Publi C Static Boolean put (String key, Object value, Integer expire) {return Cachedclient.set (key, value, expire); /** * Function Description: Replace a cached data, if there is a key replacement, otherwise return false * @param key * @param value * @return operation result/public static Boolea
N Replace (String key, Object value) {return Cachedclient.replace (key, value); /** * Function Description: Replace a cached data, if there is a key replacement, otherwise return false * @param key * @param value * @param cache time * @return Operation result * * * Publ
IC Static Boolean replace (String key, Object value, Integer expire) {return Cachedclient.replace (key, value, expire); /** * Function Description: Get a cached data according to key * @param key * @return operation result/public static Object getting (String key) {return cache
Dclient.get (key);
/** * Feature Description: Refresh all cache (that is, all cache is set to expire, later will overwrite these expired cached data) * @return operation result/public static Boolean Flushall () {
return Cachedclient.flushall (); /*** * Feature Description: Deletes a cached data according to key * @return Operation result * * public static Boolean delete (String key) {return cachedclient.de
Lete (key);
}
}
Here are examples to illustrate each function
1. Add a cache data, do not set the cache time--add method
Memcachedutil.add ("Lkx", "element sword step Green dust Test memcached cache with-do not set the expiration Time");
System.out.println ("Lkx---" + memcachedutil.get ("Lkx"));
The console output is as follows:
lkx---element sword step green dust Test memcached Cache-do not set expiration time
2. Add a cache data, set the cache time--set method
Memcachedutil.put ("Lkx", "the element sword Step green dust Test memcached cache use-set expiration Time");
System.out.println ("Lkx---" + memcachedutil.get ("Lkx"));
The console data information is as follows:
lkx---element sword step green dust Test memcached cache use-set expiration time to
change
the program: <span style= "FONT-SIZE:18PX;" ></span><pre name= "code" class= "Java" >memcachedutil.put ("Lkx", " Vegetarian Sword Step Green dust Test memcached cache with-set expiration Time Sssssssss ");
System.out.println ("Lkx---" + memcachedutil.get ("Lkx"));
The console data information is as follows:
lkx---element sword step green dust Test memcached Cache-Set Expiration time Sssssssss
This time we can compare the difference between put and add, the most obvious difference between set and add is that if the key is already existing set is to update the original data, and add is not.
3. Modify the data in the cache
Memcachedutil.replace ("Lkx", "element sword step Green dust Test memcached cache with-set expiration time Replace");
System.out.println ("Lkx---" + memcachedutil.get ("Lkx"));
The console output information is as follows:
lkx---element sword step green dust Test memcached Cache-Set Expiration time replace
The use of replace is similar to set, but when key does not exist, replace is powerless.
4. Delete data in the cache
Memcachedutil.delete ("Lkx");
System.out.println ("Lkx---" + memcachedutil.get ("Lkx"));
The console output information is as follows:
lkx---NULL
5. Mark all data in the cache as expired
Memcachedutil.flushall ();
System.out.println ("Lkx---" + memcachedutil.get ("Lkx"));
The console output information is as follows:
<span style= "FONT-SIZE:18PX;" ></span><pre name= "code" class= "java" >lkx---NULL
Well, the above basic can deal with the development of some caching problems.