xmemcached 動態(tài)增加節(jié)點
來源:程序員人生 發(fā)布時間:2015-01-26 08:51:21 閱讀次數(shù):3404次
1.介紹
支持所有的基于文本的協(xié)議和2進制memcached的基礎(chǔ)協(xié)議,目前,包括獲得/設(shè)置獲得,添加刪除添加替換、prepend、CAS、多得到/得到增加下降flush_all統(tǒng)計等。
2.依賴
如果是maven工程
<dependency>
<groupId>com.googlecode.xmemcached</groupId>
<artifactId>xmemcached</artifactId>
<version>${version}</version>
</dependency>
3.例子
MemcachedClient 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);
//Touch cache item ,update it's expire time to 10 seconds.
boolean success=client.touch("key",10);
//delete value
client.delete("key");
4.Weighted Server(節(jié)點權(quán)重)
設(shè)置 localhost:12000 的權(quán)重為1,設(shè)置 localhost:12001 的權(quán)重為3.
MemcachedClientBuilder builder = new XMemcachedClientBuilder(AddrUtil.getAddresses("localhost:12000 localhost:12001"),new int[]{1,3});
MemcachedClient memcachedClient=builder.build();
可以通過jmx動態(tài)改變權(quán)重
public interface XMemcachedClientMBean{
....
/**
* Set a memcached server's weight
*
* @param server
* @param weight
*/
public void setServerWeight(String server, int weight);
}
5. 獲得所有key
MemcachedClient client=...
KeyIterator it=client.getKeyIterator(AddrUtil.getOneAddress("localhost:11211"));
while(it.hasNext())
{
String key=it.next();
}
6.SASL認證
Memcached客戶端1.4.3支持SASL認證。2進制協(xié)議是有效的。xmemcached 1.2.5支持此功能。如果memcached服務(wù)器啟用SASL使用CRAM-MD5或普通的機制,設(shè)置用戶名為“cacheuser”和密碼為“123456”,那末你可使用xmemcached這樣:
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();
7.使用計數(shù)器來增加/減少。
您可使用增加/減少memcachedclient的方法來增加或減少計數(shù)器,但有1個計數(shù)器xmemcached封裝增加/減少的方法,你可使用計數(shù)器就像AtomicLong:
Counter counter=client.getCounter("counter",0);
counter.incrementAndGet();
counter.decrementAndGet();
counter.addAndGet(-10);
8. 2進制協(xié)議。
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();
9.Kestrel
MemcachedClientBuilder builder = new XMemcachedClientBuilder(AddrUtil.getAddresses("localhost:12000 localhost:12001"),new int[]{1,3});
builder.setCommandFactory(new KestrelCommandFactory());
MemcachedClient memcachedClient=builder.build();
// 存儲字符串
client.setPrimitiveAsString(true);
10.動態(tài)增加節(jié)點
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");
11.設(shè)置連接池
MemcachedClientBuilder builder = new XMemcachedClientBuilder(AddrUtil.getAddresses("localhost:12000"));
builder.setConnectionPoolSize(5); //set connection pool size to five
12 Cas操作
class CASThread extends Thread {
static final class IncrmentOperation implements CASOperation<Integer> {
/*
*Max repeat times.if repeat times is great than this value,
*xmemcached will throw a TimeoutException.
*/
@Override
public int getMaxTries() {
return Integer.MAX_VALUE;
}
//increase current value
@Override
public Integer getNewValue(long currentCAS, Integer currentValue) {
return currentValue + 1; // current value + 1
}
}
private XMemcachedClient mc;
private CountDownLatch cd;
public CASThread(XMemcachedClient mc, CountDownLatch cdl) {
super();
this.mc = mc;
this.cd = cdl;
}
public void run() {
try {
//do the cas operation
if (mc.cas("a", 0, new IncrmentOperation()))
this.cd.countDown();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class CASTest {
public static void main(String[] args) throws Exception {
if (args.length < 2) {
System.err.println("Usage:java CASTest [threadNum] [server]");
System.exit(1);
}
//threads num
int NUM = Integer.parseInt(args[0]);
XMemcachedClient mc = new XMemcachedClient(AddrUtil.getAddresses(args[1]));
//initial value is 0
mc.set("a", 0, 0);
CountDownLatch cdl = new CountDownLatch(NUM);
long start = System.currentTimeMillis();
//start NUM threads to increment the value
for (int i = 0; i < NUM; i++)
new CASThread(mc, cdl).start();
cdl.await();
System.out.println("test cas,timed:"
+ (System.currentTimeMillis() - start));
System.out.println("result=" + mc.get("a"));
mc.shutdown();
}
}
生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對您的學(xué)習(xí)有所幫助,可以手機掃描二維碼進行捐贈