標籤:public redist another expired package oid dcl unix時間 大於
package memcached;import java.io.IOException;import java.net.InetSocketAddress;import net.spy.memcached.MemcachedClient;public class Client {private final int expiredSeconds = 100;private final InetSocketAddress server = new InetSocketAddress("127.0.0.1",10101);private MemcachedClient memcachedClient;public void init() {try {memcachedClient = new MemcachedClient(server);} catch (IOException e) {e.printStackTrace();}}public void put(String key, Object obj) {memcachedClient.set(key, expiredSeconds, obj);}public void delete(String key) {memcachedClient.delete(key);}public Object get(String key) {return memcachedClient.get(key);}public static void main(String args[]) {Client c = new Client();c.init();long begin = System.currentTimeMillis();for(int i=0;i<100;i++) {c.put(i+"", i);System.out.println(c.get(i+""));}System.out.println((System.currentTimeMillis()-begin)+"ms");}}
第一是設定到期時間。單位為秒,這裡注意在put的時候。該值作為set的一個參數,可是假設該值大於30天的秒數的話。就會當成是unix時間來設定到期時間而不是offset。
第二行是本地的一個memcached的server地址和port,調用MemcacedClient執行個體化一個MemcachedClient對象,以下就自己定義put。get,delete經常用法就能夠了
直接結果例如以下:
2014-08-22 09:35:01.891 INFO net.spy.memcached.MemcachedConnection: Added {QA sa=/127.0.0.1:10101, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue2014-08-22 09:35:01.902 WARN net.spy.memcached.MemcachedConnection: Could not redistribute to another node, retrying primary node for 0.2014-08-22 09:35:01.904 WARN net.spy.memcached.MemcachedConnection: Could not redistribute to another node, retrying primary node for 0.012345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989992ms
net spy memcached 使用demo