您可以使用Memcached的。 Java有不同的memcached客戶端 https://code.google.com/p/memcached/wiki/Clients#Java
我已經使用spymemcached客戶端。 來自實例:https://code.google.com/p/spymemcached/wiki/Examples
//獲取連接到多臺服務器分佈式緩存客戶端
MemcachedClient c=new MemcachedClient(
AddrUtil.getAddresses("server1:11211 server2:11211"));
//嘗試得到一個值,最多5秒,並取消,如果它不返回
Object myObj=null;
Future<Object> f=c.asyncGet("someKey");
try {
myObj=f.get(5, TimeUnit.SECONDS);
} catch(TimeoutException e) {
// Since we don't need this, go ahead and cancel the operation. This
// is not strictly necessary, but it'll save some work on the server.
f.cancel(false);
// Do other timeout related stuff
}
在同樣的方式,你可以用不同的TTL設定值
Future<Object> f=c.set(key, ttl, data);
在dropwizard可以實現管理接口initalizing和回採memchached客戶 http://dropwizard.github.io/dropwizard/manual/core.html#managed-objects
public class MemcachedManger implements Managed {
private MemcachedClient client;
public MemcachedManger() {
// write a constructor for initializing custom dependencies of this class
}
@Override
public void start() throws Exception {
// create new object of memcachedClient
this.client=new MemcachedClient(AddrUtil.getAddresses("server1:11211 server2:11211"));
}
@Override
public void stop() throws Exception {
client.shutdown();
}
// define get and put and any other custom methods using functionality provided by spymemchached client
}