XMemcached Introduction
XMemcached is a new java memcached client. Maybe you don't kNow "memcached" so far,you can check . It is a free & open source,high-performance,distributed memory object caching system,generic in nature,but intended for use in speeding up dynamic web applications by alleviating database load. And many users use it as memory database too. Memcached talk with client with self define protocol,XMemcached is a java client for it.
There are two java clients before: the official client base on traditional blocked IO,maintained by ,the based on java NIO,maintained by Dustin Sallings. And there are some improved version base on them too. So,what's advantage of XMemcached,why we need a new java client for Memcached?
XMemcached Features
High performance
XMemcached is a client which base on java NIO too,Java NIO is efficient (special under high concurrent),and use less resource than traditional blocked IO. The traditional blocked IO need create some connections to build a connection pool for improve efficiency. But NIO only need one connection (sure,NIO also support pool management),reduce the cost for thread creation and switch,it is more obvious under hight concurrent. So the performance of XMemcached and Spymemcached are very excellent,and in some point,XMemcached is more excellent than Spymemcached,you can check for details.
Binary and Text protocol
XMemcached support all protocols of memcached,also included from Memcached 1.4.0.
Client distribution
Memcached supports distribution by client,and XMemcached implements it,and supply implement of consistent hash algorithm.
Weighted server
XMemcached can adjust weight of node for balance the load of memcached server,the weight is more high,the memcached server will store more data,and receive more load.
Dynamically add/remove server
XMemcached can dynamically add/remove server,by JMX or programming,it is easy to extend server or replace server.
JMX Monitor/Control
You can monitor or control XMemcached client by JMX,you can set some parameters,view the STAT. data,add or remove server etc.
Integration with Spring and Hibernate-memcached
Just like many projects,XMemcached also support integration with Spring framework. is a open source project,it can use memcached as secondary cache of hibernate,the default is Spymemcached,you can use XMemcached too.
NIO Connection Pool
As mentioned before,Java NIO will use one connection for one memcached server at most time,but XMemcached can also support connection pool. You can create a few connections to build a connection pool to one memcached server,it can improve the performance under high concurrent environment,and it is transparent to user. You must assure the data's independence or synchronization of data,there is no synchronization between connections to a server,you must make sure the data are all independent,or you can use CAS for atomic operation.
Expendable
XMemcached is implemented under java NIO framework,with clear architecture. (yanf4j is combined into XMemcached after XMemcached 1.2.5). You can find the classes UML diagram of XMemcached here We will show you some examples from simple to complex,then you can learn XMemcached easily. You must download dependence or download whole XMemcached package with dependence before test following code. If you build your project with maven,you can only add dependency to use XMemcached (only for 1.2.5+) As the general user,you need to add/get data from memcached at most time. If we have a memcached server,the IP address or host name is "host",the service port is 11211,you can check this simple example: Note: For save time,please import packages by yourself or by IDE. Since XMemcachedClient has many options to create,there is a XMemcachedClientBuilder class to help build MemcachedClient. The MemcachedClient is a main interface,you can find almost methods in this class. XMemcachedClient is a implement of MemcachedClient. When you use XMemcachedClientBuilder,the parameter of memcached server list should be a string which just like "host1:port1 host2:port2 …" ,the AddrUtil.getAddresses will get IP addresses list after parse it. You can use "set" to store data,it has 3 parameters,the first is key name,the second is expire time (second),after the expire time,you can't find the key in memcached server. The third parameter is the data for store,it can be any java serialized object. You can use "get" with a key name for retrieve data. If you will remove stored data,you can use "delete" with a key. Since XMemcached is based on NIO,so the process of communication is asynchronous,When the client send a request to memcached server,you can't kNow how long you will receive the response,client can only wait,so there is a concept of timeout. After send a request,the client will regard it is Failed after some time if no response. The default timeout value is 5 second(since 1.3.8,before is 1 second). The above example use default timeout time for all calls. If you need different timeout,you can use like this: It will timeout after 3 seconds,you will get a TimeoutException if no response in 3 second,you need handle the exception by your code. And you need handle InterruptException too,because the method use CountDownLatch.await(timeout) internally. And if MemcachedException occurred,it means some exceptions in XMemcached internal,just like encode/decode exception,or network Failed. Memcached 1.4 supports touch protocol to update cache item's expire time using key without value. Xmemcached 1.3.6 supports binary touch and getAndTouch commands,and xmemcached 1.3.8 supports text touch command: The distribution of Memcached is implement by client side,Client will get a memcached server by hash of key,then store the value to the server. XMemcached support client distribution strategy,the default strategy is Standard Hash: "hash(key) mod server_count",you need do nothing if use default strategy. XMemcached also support ,it need set by method: You can check "Integration with Spring Framework" for configuration. XMemcached supply another hash algorithm -- Election Hash,you can use it in some case,just like: Memcached implement atomic update by CAS protocol,actually it is compare and set,the theory like Optimistic locking,it will bring a cas value when request to store a data,memcached will check the cas value with current cas value,it will overwrite old data if the value is same,otherwise it Failed. It is very useful in concurrent environment. XMemcached support CAS protocol,whatever text protocol or binary protocol. CAS protocol has two steps: get CAS value and try update,so a typical use case should like: First you can get a GetsResponse by "gets" method,this object include the stored data and cas value,then try to update the value by "cas" method,it will print "cas error" if Failed. ObvIoUs,it is a little complicated. If you will try many atomic update you need a loop,so XMemcached supply a CASOperation interface to implement it,it allows you try N times to atomic update the data of the key,you don't need get cas value by call "gets" again,just like this: CASOperation interface only has two method,one method "getMaxTries" is used to set max try times,it is 1 time by default,if bigger than "Max Tries",it will throw a TimeoutException,if you will try more times,just set a big value; another method is "getNewValue",the returned value will be stored if not Failed,the parameters of this method are get from last GetsResponse result by call "gets". Here is more example,show how to call methods of MemcachedClient:
User Guide
Dependence
If you use maven
Simple Example
stem
stemstemprintstacktracestemprintstacktracenorestemprintstacktrace
touch and getAndTouch
Client distribution
CAS Operation
stem
()
More Examples