MemcachedClient usage instructions, memcachedclient
The previous article introduced the basic usage of Memcached, Memcached User Manual. The following describes how to operate memcached in java. The java_memcached-release_2.6.6 is used.
I. Usage
Create a project and add relevant jar packages:
The code is directly written, and the comments are very detailed. You don't need to say much.
Package www.xufei.com; import com. danga. memCached. memCachedClient; import com. danga. memCached. sockIOPool; public class MemcachedDemo {public static void main (String [] args) {// memcached Server String [] servers = {"127.0.0.1: 11211", "192.168.1.3: 11211 "}; /*** set the weights of available cache servers in the connection pool, which correspond to the server array locations one by one */Integer [] weights = {1, 2 }; /*** this class is used to create a management client and server communication connection pool. The main work of the client includes data communication, server locating, and hash code generation. * Obtain the single-state method of the connection pool. This method has an overload method getInstance (String poolName). Each poolName constructs only one SockIOPool instance. * The default poolName is default. */SockIOPool = SockIOPool. getInstance (); // set the memcached server pool. setServers (servers); // sets the memcached server weight pool. setWeights (weights); // sets the fault tolerance switch to TRUE. When the current socket is unavailable, the program automatically finds available connections and returns the result. Otherwise, NULL is returned. The default status is true, we recommend that you keep the default pool. setFailover (true); // set the pool of available connections for each cache server at the beginning. setInitConn (10); // sets the pool of the minimum number of available connections for each server. setMinConn (5); // set the maximum number of available connections for each server pool. setMaxConn (250);/*** set the sleep time of the connection pool maintenance thread * to 0, and the maintenance thread does not start * The maintenance thread outputs so mainly through log To control connection creation and closure. */Pool. setMaintSleep (30);/*** set whether to use the Nagle algorithm, because our communication data volume is usually large (relative to TCP Control Data) and requires timely response, therefore, set this value to false (true by default) */pool. setNagle (false);/*** set the socket read wait timeout value */pool. setSocketTO (3000);/*** sets the connection heartbeat monitoring switch. * If it is set to true, connection monitoring is required for each communication, which doubles the number of communications and increases network load. Therefore, this parameter should be set to TRUE when HA requirements are high, the default status is false. */Pool. setAliveCheck (true);/*** call this method after setting the pool parameter to start the pool. */Pool. initialize ();/*** create a memcached client. All data operations on memcached are in this class */MemCachedClient memCachedClient = new MemCachedClient (); /*** store a username with the value of Liu Dehua. If the storage succeeds, true */boolean success = memCachedClient is returned. set ("username", "Andy Lau"); System. out. println (success);/*** obtain a data key username from the cache */Object o = memCachedClient. get ("username"); System. out. println (o);/*** defines a p object. The Persion class must implement the Serializable interface */Persion P = new Persion (); p. setId ("1"); p. setName ("Jay Chou");/*** cache a p object */memCachedClient. set ("p1", p);/*** get the cached p object */Persion p1 = (Persion) memCachedClient. get ("p1"); System. out. println (p1.getName ();/*** call the add method to add an object whose key is p1. A value of 123 cannot be added to the cache, because p1 has been added once */memCachedClient. add ("p1", 123); // error! The value of p1 cannot be updated. out. println (memCachedClient. get ("p1"); // or the person object/*** use the set method to update p1 */memCachedClient. set ("p1", 123); System. out. println (memCachedClient. get ("p1"); // output 123/*** use the replace method to update p1 */memCachedClient. replace ("p1", 456); System. out. println (memCachedClient. get ("p1"); // output 456/*** the replace method can be used to update p2. The cache does not contain data whose key is p2 and cannot be updated, does not add */memCachedClient. replace ("p2", 456); System. out. println (memCachedClient. get ("p2"); // output null/*** Delete cache data whose key is p1 */memCachedClient. delete ("p1"); System. out. println (memCachedClient. get ("p1"); // output null }}
Ii. Description of common methods
1SockIOPool is a socket connection pool class
SetServers (String [] servers): sets the server information array;
SetWeights (String [] weights): sets an array of server weights;
SetInitConn (int count): sets the initial number of connections;
SetMinConn (int minConn): sets the minimum number of connections;
SetMaxConn (int maxConn): sets the maximum number of connections;
SetMaxIdle (long arg0): sets the maximum processing time;
SetMaintSleep (long arg0): Sleep Time of the main thread;
Initialize (): initializes the connection pool.
2MemCachedClient class and common methods
Add (String key, Object value): add a key-value pair to the cache;
Add (String key, Object value, Date expires): add a key-value pair to the cache and set its timeout time;
Set (String key, Object value): set a key value in the cache;
Set (String key, Object value, Date expires): set a key value in the cache and set its timeout time;
Get (String key): get the value of a key.
Incr (String key): executes the + 1 operation for the value on a key;
Decr (String key): performs the-1 operation for the value on a key;
Replace (String key, String value): replace the value of a key with a new value;
Replace (String key, String value, Date expires): replace the value of a key with a new value and set its timeout time.
Delete (String key): deletes the value of a key in the cache.