Memcached Client Usage Manual

Source: Internet
Author: User
Tags failover int size memcached

Memcached Client Usage Manual
Author:cenwenchu
Email:wenchu.cenwc@alibaba-inc.com
blog:http://blog.csdn.net/cenwenchu79/
project:http://code.google.com/p/memcache-client-forjava/


Cache Client Interface Definition

Figure 1 Memcached Cache Client interface class diagram

The Icache and Imemcachedcache interfaces are the cache's underlying interfaces, which define the basic cache operation, detailed in the appendix, and used in the form of demo and usage notices. Icachemanager is the cache lifecycle management class, an application only needs a icachemanager to manage all the cache, specifically described in the Cache Demo Cache Manager Introduction.

Cache Client dependencies and configuration
Cache Client Third party dependent
Commons-logging-1.0.4.jar or High version
Log4j-1.2.12.jar or High version
Codehaus/woodstox/wstx-asl-3.2.1.jar or High version
Codehaus/staxapi/stax-api-1.0.1.jar or High version

The 2.5.2 version also requires Caucho/hessian/hessian-3.2.0.jar or high

Cache Client supports default (find Memcached.xml as client configuration in classpath) and specifies configuration file. The configuration file is the basis for the normal operation of the cache client, and if the system is to use cache client in operation, it must initialize the cache client component (read the configuration file, create the connection pool, and so on) before use (usually preferably when the application starts). You can refer to the demo in the later chapters for specific use.
Cache Client Single Clients configuration
<?xml version= "1.0" encoding= "UTF-8"?>
<memcached>
<client name= "Mclient0" compressenable= "true" defaultencoding= "UTF-8" socketpool= "pool0" >
<errorHandler>com.alisoft.xplatform.asf.cache.memcached.MemcachedErrorHandler</errorHandler> </ Client>
<socketpool name= "Pool0" failover= "true" initconn= "5" minconn= "5" maxconn= "" "Maintsleep=" 5000 "
Nagle= "false" socketto= "3000" alivecheck= "true" >
<servers>10.2.224.36:33001,10.2.224.46:33001</servers>
<weights>3,7</weights>
</socketpool>
</memcached>
Create a memcached label.
Create a label for the client.
Attention:
The Name property is the unique identification of the cache used in the program.
The Socketpool property will be associated with the following socketpool configuration.
ErrorHandler is optional and is used to handle error situations. Note that in the tag, do not use spaces or the TAB key.
Create a Socketpool label.
Attention:
The Name property is associated with the Socketpool property in the client configuration.
The Maintsleep property is the check interval for the background thread management Socketio pool, which, if set to 0, does not require the background thread to maintain the socketio thread pool, which requires management by default.
The Socketto property is the socket operation timeout configuration, unit Ms.
The Alivecheck property indicates whether the socket state is checked before using the socket.
Create a servers label as a child label for the Socketpool. Set the Memcache server-side instance address to support multiple address settings, such as "10.2.224.36:33001" or "10.2.224.36:33001," 10.2.224.46:33002 ".
Create the weights label as a socketpool (optional), which indicates the load weight of the server instance set above. For example <weights>3,7</weights> indicates 30% load in 10.2.224.36:33001, 70% load in 10.2.224.46:33001

Well, the basic configuration is as above. Now you can go directly to the later chapters to learn to use the Memcache client, or continue to see how to configure cluster.


Cache Client cluster configuration
<?xml version= "1.0" encoding= "UTF-8"?>
<memcached>
<client name= "Mclient0" compressenable= "true" defaultencoding= "UTF-8" socketpool= "pool0" >
<errorHandler>com.alisoft.xplatform.asf.cache.memcached.MemcachedErrorHandler</errorHandler>
</client>
<client name= "Mclient0-bck" compressenable= "true" defaultencoding= "UTF-8" socketpool= "Pool0-bck" >
<errorHandler>com.alisoft.xplatform.asf.cache.memcached.MemcachedErrorHandler</errorHandler>
</client>
<socketpool name= "Pool0" failover= "true" initconn= "5" minconn= "5" maxconn= "" "Maintsleep=" 5000 "
Nagle= "false" socketto= "3000" alivecheck= "true" >
<servers>10.2.224.36:33001,10.2.224.46:33001</servers>
</socketpool>
<socketpool name= "Pool0-bck" failover= "true" initconn= "5" minconn= "5" maxconn= "" maintsleep= "5000" nagle= "false "Socketto=" 3000 alivecheck= "true" >
<servers>10.2.224.36:33002,10.2.224.46:33002</servers>
</socketpool>

<cluster name= "Cluster1" mode= "active" >//mode = Active,standby
<memCachedClients> Mclient0, mclient0-bck</memcachedclients>
</cluster>
</memcached>

Memcache is a centralized cache, so it has a single point of problem (although data can be dispersed across multiple servers, some data will be lost). To resolve a single point of issue, the Memcache client supports configuring the cluster.
Cluster configuration is simple. 1. Create cluster label 2. Create the Memcachedclients label as a child label for the cluster, and then configure the client to the Memcachedclients label. 3. Cluster mode can be configured (if the Mode property is not set, the default is active).
The current characteristics of the cluster:
Multi-node soft load balancing in cluster. (currently using a simple hash algorithm plus the remainder to distribute data)
Data is asynchronously and redundantly stored on multiple nodes. (Prevent data loss most basic requirements)
The node is not available for switching functions. (Can be diverted to other available nodes when distributed to a failed node based on an algorithm)
Data lazy Replication After node recovery is available. (when a,b two machines as a cluster, if a problem, the system will go to B to obtain data, when a normal, if the application in a did not get the data can go to B to obtain data, and copy to A, this way is a lazy copy.) )


Cache Client Demo
There is a sample package in the Google project that contains the configuration files needed for the test case and test (the specific server ports and addresses in the configuration file need to be modified).

Download the address: Http://memcache-client-forjava.googlecode.com/files/alisoft-xplatform-asf-cache-sample%282.5version%29.zip

Class Imemcachedcachetest is the cache function unit test class.
Class Stresscachetest is the cache pressure test class.
Class Memcachedclustertest is the cache cluster test class.

Cache Manager
In applications using cache client, a cache manager is required to manage the lifecycle of each cache client. Cache Manager initializes each cache client by reading the configuration, and uses the cache manager to obtain data interaction with the cache client. When Cache Manager is finished, the cache client is terminated and released. Cache Manager can also dynamically reload the configuration file to achieve dynamic expansion.
It is recommended that one application simply set up a cache manager, start the cache manager when the application is started, and end the cache manager at the end of the application. The test case code is as follows:

Static icachemanager<imemcachedcache> manager;//can be directly set to a single case

@BeforeClass
public static void Setupbeforeclass () throws Exception
{
Manager = Cacheutil.getcachemanager (Imemcachedcache.class,
MemcachedCacheManager.class.getName ()),//manager initialization, can be configured to replace CacheManager implementation
Manager.setconfigfile ("Memcached1.xml");/Set cache client configuration file
Manager.setresponsestatinterval (5*1000)//Set cache response statistic interval, no statistics without setting
Manager.start ();//manager start
}

@AfterClass
public static void Teardownafterclass () throws Exception
{
Manager.stop ();//manager End
}


Through Manager.reload ("Memcached_cluster2.xml"), dynamic expansion can be achieved, while supporting the memcached_cluster2.xml as http://10.2.226.41/sip/ Memcached_cluster2.xml, implement remote access configuration. For specific use, refer to the code in the cluster test case. (Note the sleep code in the test case, which is the delay of the system at shutdown and startup)

Use attention
When you use the Clear method, if you get the data immediately, you may get the deleted data because of the recycle speed problem, you need some delay, and sleep is the reason to avoid the problem.
The manager's reload will reload the configuration, but the server-side data will not be deleted, so you need to be aware of the problem of copying the cluster data to each other in the new configuration. At the same time reload later for the cache client needs to regain the object, otherwise reserved or the original cache client, will not be able to use. (Data replication takes the form of lazy, if you need to redistribute all immediately, you can implement the manager's Clustercopy interface)
For some scenarios of get,put joint operations, it is recommended to use counters to implement atomic operations. (counters need to be operated via GETCOUNTER,STORECOUNTER,INCR,DECR and so on, not ordinary get,put operations)
You can use Add,replace to meet scenarios that require different policies for whether content is stored.
In cases where the local cache and memcached cache combinations are used, when the data is cached locally, if modified, the local buffer is invalidated, but the local cache cannot be notified to other applications or to other servers.
Cluster active and standby two modes, the former speed may be slightly slow in some cases (when the key does not exist in any node of the cluster, the active mode will try to get the data of two nodes), but with data recovery function, the latter is faster, but no data recovery function.


Appendix:
Interface Definition Description:
/**
* Cache Unified Interface
* @author WENCHU.CENWC
*
*/
public interface icache<k,v>
{
/**
* Save Data
* @param key
* @param value
* @return
*/
Public V-Put (K key,v value);

/**
* Save the valid data
* @param key
* @param value
* @param validity period (take the client time)
* @return
*/
Public V-Put (K key,v value, Date expiry);

/**
* Save the valid data
* @param key
* @param value
* @param setting is valid for TTL seconds from the current time.
* @return
*/
Public V-Put (K key,v value, int TTL);

/**
* Get Cached data
* @param key
* @return
*/
Public V get (K key);

/**
* Move out cached data
* @param key
* @return
*/
Public V Remove (K key);

/**
* Delete all cached data
* @return
*/
public boolean clear ();

/**
* Number of cached data (not currently supported by the Memcached interface)
* @return
*/
public int size ();

/**
* Cache all key Collections
* @return
*/
Public set<k> keyset ();

/**
* A collection of all of the cached value
* @return
*/
Public collection<v> values ();

/**
* Contains the data for the specified key
* @param key
* @return
*/
public boolean ContainsKey (K key);

/**
* Release Cache-occupied resources
*/
public void Destroy ();
}

/**
* Memcached Cache Interface Definition
* @author wenchu.cenwc<wenchu.cenwc@alibaba-inc.com>
*/
Public interface Imemcachedcache extends icache<string,object>
{
/**
* Reduce the memcache interaction caused by frequent performance loss, so the use of local cache combined with Memcache way
* @param key
* @param local cache of valid seconds for this data
* @return
*/
Public Object Get (String key,int Localttl);

/**
* Get multiple keys corresponding to the value
* @param keys
* @return
*/
Public object[] Getmultiarray (string[] keys);
/**
* Get multiple keys corresponding to the Key&value Entrys
* @param keys
* @return
*/
Public map<string,object> Getmulti (string[] keys);


/**
* Key corresponds to a counter to increase the number of INC
* @param key
* @param Inc
* @return
*/
Public long INCR (String Key,long Inc.);

/**
* Key corresponds to a counter, to achieve the reduction of the number of DECR
* @param key
* @param DECR
* @return
*/
Public long DECR (String key,long decr);

/**
* Key corresponds to a counter to increase the number of INC
* @param key
* @param Inc
* @return
*/
Public long ADDORINCR (String Key,long Inc.);

/**
* Key corresponds to a counter, to achieve the reduction of the number of DECR
* @param key
* @param DECR
* @return
*/
Public long ADDORDECR (String key,long decr);

/**
* Storage Counters
* @param key
* @param count
*/
public void Storecounter (String key,long count);

/**
* Get registers,-1 indicates no
* @param key
*/
Public long Getcounter (String key);


/**
* If the key returned by this interface is in fast mode,
* Then the returned key may have been cleared or invalidated, but there are traces in memory, and if it is a non-fast mode, it will return exactly, but less efficient
* @param whether it is necessary to check whether the key exists
* @return
*/
Public Set<string> Keyset (Boolean fast);

/**
* Statistics The slab of the server
* @return
*/
Public memcachestatsslab[] Statsslabs ();

/**
* Statistics on the use of memcache
* @return
*/
Public memcachestats[] Stats ();

/**
* Statistics on the storage of items
* @param servers
* @return
*/
@SuppressWarnings ("Unchecked")
Public Map statsitems ();

/**
* Statistics Cache response Time (must be set statisticsinterval greater than 0 to start statistics)
* @return
*/
Public Memcachedresponse statcacheresponse ();

/**
* Set statistic time in seconds
* @param checkinterval
*/
public void Setstatisticsinterval (long checkinterval);

/**
* Save the data, if the key does not exist in the memcache, otherwise the save is unsuccessful
* @param key
* @param value
* @return
*/
Public boolean Add (String key,object value);

/**
* Save the valid data, if the key does not exist in the memcache, otherwise save unsuccessful
* @param key
* @param value
* @param validity period
* @return
*/
Public boolean Add (String key,object value, Date expiry);


/**
* Save the data, if the key must exist in the memcache, otherwise the save is unsuccessful
* @param key
* @param value
* @return
*/
public boolean replace (String key,object value);

/**
* Save the valid data, if the key must exist in the memcache, otherwise the save is unsuccessful
* @param key
* @param value
* @param validity period
* @return
*/
public boolean replace (String key,object value, Date expiry);

/**
* The data is stored asynchronously, now returned immediately, and later stored in the data
* @param key
* @param value
*/
public void Asynput (String key,object value);


/**
* Asynchronous decrement counter, does not guarantee the exhaustion success
* @param key
* @param DECR
*/
public void Asynaddordecr (String key,long decr);

/**
* Asynchronous cumulative counter, does not guarantee cumulative success
* @param key
* @param incr
*/
public void asynaddorincr (String key,long incr);

/**
* Asynchronous decrement counter, does not guarantee the exhaustion success
* @param key
* @param DECR
*/
public void Asyndecr (String key,long decr);

/**
* Asynchronous cumulative counter, does not guarantee cumulative success
* @param key
* @param incr
*/
public void asynincr (String key,long incr);

/**
* Asynchronous storage counters, does not guarantee the success of the Save
* @param key
* @param count
*/
public void Asynstorecounter (String key,long count);
}

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.