|
Getting_Started
IntroductionSupports all memcached text based protocols and binary base protocols currently, include get/gets、set、add、replace、delete、append、prepend、cas、multi get/gets、incr、decr、version、stats、flush_all etc. DependenciesXmemcached use slf4j for logging. If you are using maven to build your project,you would just add a dependence with xmemcached:
Simple ExampleMemcachedClient client=new XMemcachedClient("host",11211);
//store a value for one hour(synchronously).
client.set("key",3600,someObject);
//Retrieve a value.(synchronously).
Object someObject=client.get("key");
//Retrieve a value.(synchronously),operation timeout two seconds.
someObject=client.get("key",2000);
//delete value
client.delete("key");
Weighted Server MemcachedClientBuilder builder = new XMemcachedClientBuilder(AddrUtil.getAddresses("localhost:12000 localhost:12001"),new int[]{1,3});
MemcachedClient memcachedClient=builder.build();It sets "localhost:12000" weight to 1,and "localhost:12000" weight to 3.You can change the weight dynamically through JMX public interface XMemcachedClientMBean{
....
/**
* Set a memcached server's weight
*
* @param server
* @param weight
*/
public void setServerWeight(String server, int weight);
}Key IteratorThis feature is only avaliable on xmemcached 1.2.2 when using text protocol.You can get a KeyIterator through: MemcachedClient client=...
KeyIterator it=client.getKeyIterator(AddrUtil.getOneAddress("localhost:11211"));
while(it.hasNext())
{
String key=it.next();
}SASL authenticationMemcached 1.4.3 has supported SASL authentication for client.It is only valid for binary protocol.Xmemcached 1.2.5 has supported this feature.If the memcached server enable SASL and use CRAM-MD5 or PLAIN mechanisms, set username as "cacheuser" and password as "123456",then you can use xmemcached like this: MemcachedClientBuilder builder = new XMemcachedClientBuilder(
AddrUtil.getAddresses(server));
builder.addAuthInfo(AddrUtil.getOneAddress(server), AuthInfo
.typical("cacheuser", "password"));
// Must use binary protocol
builder.setCommandFactory(new BinaryCommandFactory());
MemcachedClient client=builder.build();Use Counter to incr/decrYou can use MemcachedClient's incr/decr methods to increase or decrease counter,but xmemcached has a counter which encapsulate the incr/decr methods,you can use counter just like AtomicLong: Counter counter=client.getCounter("counter",0);
counter.incrementAndGet();
counter.decrementAndGet();
counter.addAndGet(-10);Use binary protocolIf you use memcached 1.4.0 or later version,you may want to use binary protocol insteadof text protocol which is used by xmemcached default.You can set it by MemcachedClientBuilder builder = new XMemcachedClientBuilder(AddrUtil.getAddresses("localhost:12000 localhost:12001"),new int[]{1,3});
builder.setCommandFactory(new BinaryCommandFactory());//use binary protocol
MemcachedClient memcachedClient=builder.build();
Then xmemcached will use binary protocol to encode/decode commands. Talk with KestrelKestrel is open source MQ server written in scala.It supports memcached text protocol,but not all compatible.For example,it does'n support "flag",you can not store java serializable object when use many java memcached clients.XMemcached add a KestrelCommandFactory to support Kestrel,If you use it,you could have those benefit:
To use KestrelCommandFactory : MemcachedClientBuilder builder = new XMemcachedClientBuilder(AddrUtil.getAddresses("localhost:12000 localhost:12001"),new int[]{1,3});
builder.setCommandFactory(new KestrelCommandFactory());
MemcachedClient memcachedClient=builder.build();Additional about store java serializable object,because kestrel doesn't support "flag",so XMemcached add 4-bytes as flag before stored value.If your applications all use xmemcached as client,it is no problem;If not,There is a compatible problem,so xmemcached allow you to disable this feature byclient.setPrimitiveAsString(true); Set it true means xmemcached will store all java primitive type as string. Talking with Tokyo TyrantUsing TokyoTyrantTranscoder when you want to talk with TokyoTyrant,Just like Kestrel,TokyoTyrant doesn't support "flag",So TokyoTyrantTranscoder add 4-bytes flag before the value to store in TokyoTyrant. builder.setTranscoder(new TokyoTyrantTranscoder()); Add/Remove memcached servers dynamicallyXmemcached supports add or remove memcached servers dynamically through programming or JMX MBeans.Add or remove memcached servers example MemcachedClient client=new XMemcachedClient(AddrUtil.getAddresses("server1:11211 server2:11211"));
//Add two new memcached nodes
client.addServer("server3:11211 server4:11211");
//Remove memcached servers
client.removeServer("server1:11211 server2:11211");Nio connection poolIn a high concurrent enviroment,you may want to pool memcached clients.But a xmemcached client has to start a reactor thread and some thread pools,if you create too many clients,the cost is very large.Xmemcached supports connection pool instreadof client pool.you can create more connections to one or more memcached servers,and these connections share the same reactor and thread pools.Note,Your application must ensure updating data is synchronized between connections which connected to the same memcached server.Seting pool size:
Using CASUsing CASOperation instead of using gets and cas methods.Here is a simple example,start NUM threads to increase the value of key "a"
Intergrating Spring frameworkPlease see Spring_Integration |
it is very well.
是否能提供一个操作Kestrel 收/发的完整示例呢? 让别人更好的使用 xmemcached。
Your application must ensure updating data is synchronized between connections which connected to the same memcached server.
Do you mean have to synchronize all calls to set if connectionPoolSize is more than 1, because I have no way to know if call is going to the same server or not.