public class GrizzlyMemcachedCache<K,V> extends Object implements MemcachedCache<K,V>, ZooKeeperSupportCache
MemcachedCache based on Grizzly
Basically, this class use BaseObjectPool for pooling connections of the memcached server
and ConsistentHashStore for selecting the memcached server corresponding to the given key.
When a Cache operation is called,
1. finding the correct server by consistent hashing
2. borrowing the connection from the connection pool
3. queueing request and sending packets to the memcached server and waiting for notification
4. being waken by Grizzly filter when the response is arrived
5. returning the connection to the pool
For the failback of the memcached server, HealthMonitorTask will be scheduled by healthMonitorIntervalInSecs.
If connecting and writing are failed, this cache retries failure operations by retryCount.
If retrials also failed, the server will be regarded as not valid and removed in ConsistentHashStore.
Sometimes, automatical changes of the server list can cause stale cache data at runtime.
So this cache provides failover flag which can turn off the failover/failback.
This cache also supports bulk operations such as setMulti(java.util.Map<K, V>, int) as well as getMulti(java.util.Set<K>).
Example of use:
// creates a CacheManager
final GrizzlyMemcachedCacheManager manager = new GrizzlyMemcachedCacheManager.Builder().build();
// creates a CacheBuilder
final GrizzlyMemcachedCache.Builder<String, String> builder = manager.createCacheBuilder("USER");
// sets initial servers
builder.servers(initServerSet);
// creates the specific Cache
final MemcachedCache<String, String> userCache = builder.build();
// if you need to add another server
userCache.addServer(anotherServerAddress);
// cache operations
boolean result = userCache.set("name", "foo", expirationTimeoutInSec, false);
String value = userCache.get("name", false);
// ...
// shuts down
manager.shutdown();
| Modifier and Type | Class and Description |
|---|---|
static class |
GrizzlyMemcachedCache.Builder<K,V> |
| Modifier and Type | Field and Description |
|---|---|
static String |
CONNECTION_POOL_ATTRIBUTE_NAME |
| Modifier and Type | Method and Description |
|---|---|
boolean |
add(K key,
V value,
int expirationInSecs,
boolean noReply) |
boolean |
add(K key,
V value,
int expirationInSecs,
boolean noReply,
long writeTimeoutInMillis,
long responseTimeoutInMillis) |
boolean |
addServer(SocketAddress serverAddress)
Add a specific server in this cache
|
void |
addZooKeeperListener(BarrierListener listener)
Add the custom
BarrierListener
The given listener will be called after cache's default listener will be completed. |
boolean |
append(K key,
V value,
boolean noReply) |
boolean |
append(K key,
V value,
boolean noReply,
long writeTimeoutInMillis,
long responseTimeoutInMillis) |
boolean |
cas(K key,
V value,
int expirationInSecs,
long cas,
boolean noReply) |
boolean |
cas(K key,
V value,
int expirationInSecs,
long cas,
boolean noReply,
long writeTimeoutInMillis,
long responseTimeoutInMillis) |
Map<K,Boolean> |
casMulti(Map<K,ValueWithCas<V>> map,
int expirationInSecs) |
Map<K,Boolean> |
casMulti(Map<K,ValueWithCas<V>> map,
int expirationInSecs,
long writeTimeoutInMillis,
long responseTimeoutInMillis) |
long |
decr(K key,
long delta,
long initial,
int expirationInSecs,
boolean noReply) |
long |
decr(K key,
long delta,
long initial,
int expirationInSecs,
boolean noReply,
long writeTimeoutInMillis,
long responseTimeoutInMillis) |
boolean |
delete(K key,
boolean noReply) |
boolean |
delete(K key,
boolean noReply,
long writeTimeoutInMillis,
long responseTimeoutInMillis)
delete the item with the given key in memcached
|
Map<K,Boolean> |
deleteMulti(Set<K> keys) |
Map<K,Boolean> |
deleteMulti(Set<K> keys,
long writeTimeoutInMillis,
long responseTimeoutInMillis) |
boolean |
flushAll(SocketAddress address,
int expirationInSecs,
boolean noReply) |
boolean |
flushAll(SocketAddress address,
int expirationInSecs,
boolean noReply,
long writeTimeoutInMillis,
long responseTimeoutInMillis) |
V |
gat(K key,
int expirationInSecs,
boolean noReply) |
V |
gat(K key,
int expirationInSecs,
boolean noReply,
long writeTimeoutInMillis,
long responseTimeoutInMillis) |
V |
get(K key,
boolean noReply) |
V |
get(K key,
boolean noReply,
long writeTimeoutInMillis,
long responseTimeoutInMillis) |
List<SocketAddress> |
getCurrentServerList()
Get current server list
|
String |
getCurrentServerListFromZooKeeper()
Return the current cache server list string from the ZooKeeper server
|
ValueWithKey<K,V> |
getKey(K key,
boolean noReply) |
ValueWithKey<K,V> |
getKey(K key,
boolean noReply,
long writeTimeoutInMillis,
long responseTimeoutInMillis) |
Map<K,V> |
getMulti(Set<K> keys) |
Map<K,V> |
getMulti(Set<K> keys,
long writeTimeoutInMillis,
long responseTimeoutInMillis) |
String |
getName()
Return the name of the cache.
|
ValueWithCas<V> |
gets(K key,
boolean noReply) |
ValueWithCas<V> |
gets(K key,
boolean noReply,
long writeTimeoutInMillis,
long responseTimeoutInMillis) |
Map<K,ValueWithCas<V>> |
getsMulti(Set<K> keys) |
Map<K,ValueWithCas<V>> |
getsMulti(Set<K> keys,
long writeTimeoutInMillis,
long responseTimeoutInMillis) |
String |
getZooKeeperServerListPath()
Return the path of the cache server list which has been registered in the ZooKeeper server
|
long |
incr(K key,
long delta,
long initial,
int expirationInSecs,
boolean noReply) |
long |
incr(K key,
long delta,
long initial,
int expirationInSecs,
boolean noReply,
long writeTimeoutInMillis,
long responseTimeoutInMillis) |
boolean |
isInServerList(SocketAddress serverAddress)
Check if this cache contains the given server
|
boolean |
isZooKeeperSupported()
Check if this cache supports the ZooKeeper for synchronizing the cache server list
|
boolean |
noop(SocketAddress address) |
boolean |
noop(SocketAddress address,
long writeTimeoutInMillis,
long responseTimeoutInMillis) |
boolean |
prepend(K key,
V value,
boolean noReply) |
boolean |
prepend(K key,
V value,
boolean noReply,
long writeTimeoutInMillis,
long responseTimeoutInMillis) |
boolean |
quit(SocketAddress address,
boolean noReply) |
boolean |
quit(SocketAddress address,
boolean noReply,
long writeTimeoutInMillis,
long responseTimeoutInMillis) |
void |
removeServer(SocketAddress serverAddress)
Remove the given server in this cache
|
void |
removeZooKeeperListener(BarrierListener listener)
Remove the custom
BarrierListener
The given listener will be called after cache's default listener will be completed. |
boolean |
replace(K key,
V value,
int expirationInSecs,
boolean noReply) |
boolean |
replace(K key,
V value,
int expirationInSecs,
boolean noReply,
long writeTimeoutInMillis,
long responseTimeoutInMillis) |
String |
saslAuth(SocketAddress address,
String mechanism,
byte[] data) |
String |
saslAuth(SocketAddress address,
String mechanism,
byte[] data,
long writeTimeoutInMillis,
long responseTimeoutInMillis) |
String |
saslList(SocketAddress address) |
String |
saslList(SocketAddress address,
long writeTimeoutInMillis,
long responseTimeoutInMillis) |
String |
saslStep(SocketAddress address,
String mechanism,
byte[] data) |
String |
saslStep(SocketAddress address,
String mechanism,
byte[] data,
long writeTimeoutInMillis,
long responseTimeoutInMillis) |
boolean |
set(K key,
V value,
int expirationInSecs,
boolean noReply) |
boolean |
set(K key,
V value,
int expirationInSecs,
boolean noReply,
long writeTimeoutInMillis,
long responseTimeoutInMillis) |
boolean |
setCurrentServerListOfZooKeeper(String cacheServerList)
Set the current cache server list string with the given
cacheServerList
cacheServerList could be comma separated host:port pairs, each corresponding to a memcached server. |
Map<K,Boolean> |
setMulti(Map<K,V> map,
int expirationInSecs) |
Map<K,Boolean> |
setMulti(Map<K,V> map,
int expirationInSecs,
long writeTimeoutInMillis,
long responseTimeoutInMillis) |
void |
start()
Notifies providers to start themselves.
|
Map<String,String> |
stats(SocketAddress address) |
Map<String,String> |
stats(SocketAddress address,
long writeTimeoutInMillis,
long responseTimeoutInMillis) |
Map<String,String> |
statsItems(SocketAddress address,
String item) |
Map<String,String> |
statsItems(SocketAddress address,
String item,
long writeTimeoutInMillis,
long responseTimeoutInMillis) |
void |
stop()
Providers may be doing all sorts of exotic things and need to be able to clean up on
stop.
|
String |
toString() |
boolean |
touch(K key,
int expirationInSecs) |
boolean |
touch(K key,
int expirationInSecs,
long writeTimeoutInMillis,
long responseTimeoutInMillis) |
boolean |
verbosity(SocketAddress address,
int verbosity) |
boolean |
verbosity(SocketAddress address,
int verbosity,
long writeTimeoutInMillis,
long responseTimeoutInMillis) |
String |
version(SocketAddress address) |
String |
version(SocketAddress address,
long writeTimeoutInMillis,
long responseTimeoutInMillis) |
public static final String CONNECTION_POOL_ATTRIBUTE_NAME
public void start()
start in interface CacheLifecyclepublic void stop()
stop in interface CacheLifecyclepublic boolean addServer(SocketAddress serverAddress)
addServer in interface MemcachedCache<K,V>serverAddress - a specific server's SocketAddress to be addedserverAddress is added successfullypublic void removeServer(SocketAddress serverAddress)
removeServer in interface MemcachedCache<K,V>serverAddress - the specific server's SocketAddress to be removed in this cachepublic boolean isInServerList(SocketAddress serverAddress)
isInServerList in interface MemcachedCache<K,V>serverAddress - the specific server's SocketAddress to be checkedserverAddresspublic List<SocketAddress> getCurrentServerList()
getCurrentServerList in interface MemcachedCache<K,V>public boolean isZooKeeperSupported()
isZooKeeperSupported in interface ZooKeeperSupportCachepublic String getZooKeeperServerListPath()
getZooKeeperServerListPath in interface ZooKeeperSupportCachepublic String getCurrentServerListFromZooKeeper()
getCurrentServerListFromZooKeeper in interface ZooKeeperSupportCachepublic boolean setCurrentServerListOfZooKeeper(String cacheServerList)
cacheServerList
cacheServerList could be comma separated host:port pairs, each corresponding to a memcached server.
e.g. "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002"
Be careful that this operation will propagate cacheServerList to caches which has joinned the same cache name(scope)
because the cache list of ZooKeeper server will be changed.setCurrentServerListOfZooKeeper in interface ZooKeeperSupportCachecacheServerList - the cache server list stringpublic void addZooKeeperListener(BarrierListener listener)
BarrierListener
The given listener will be called after cache's default listener will be completed.
BarrierListener.onInit(java.lang.String, java.lang.String, byte[]) will be called when this cache will be registered in the ZooKeeper.
BarrierListener.onCommit(java.lang.String, java.lang.String, byte[]) will be called when this cache's server list will be changed in the ZooKeeper.
BarrierListener.onDestroy(java.lang.String) will be called when this cache will be unregistered in the ZooKeeper.addZooKeeperListener in interface ZooKeeperSupportCachelistener - the custom listenerpublic void removeZooKeeperListener(BarrierListener listener)
BarrierListener
The given listener will be called after cache's default listener will be completed.
BarrierListener.onInit(java.lang.String, java.lang.String, byte[]) will be called when this cache will be registered in the ZooKeeper.
BarrierListener.onCommit(java.lang.String, java.lang.String, byte[]) will be called when this cache's server list will be changed in the ZooKeeper.
BarrierListener.onDestroy(java.lang.String) will be called when this cache will be unregistered in the ZooKeeper.removeZooKeeperListener in interface ZooKeeperSupportCachelistener - the custom listener which was given by ZooKeeperSupportCache.addZooKeeperListener(org.glassfish.grizzly.memcached.zookeeper.BarrierListener)public String getName()
public boolean set(K key, V value, int expirationInSecs, boolean noReply, long writeTimeoutInMillis, long responseTimeoutInMillis)
set in interface MemcachedCache<K,V>public Map<K,Boolean> setMulti(Map<K,V> map, int expirationInSecs)
setMulti in interface MemcachedCache<K,V>public Map<K,Boolean> setMulti(Map<K,V> map, int expirationInSecs, long writeTimeoutInMillis, long responseTimeoutInMillis)
setMulti in interface MemcachedCache<K,V>public boolean add(K key, V value, int expirationInSecs, boolean noReply, long writeTimeoutInMillis, long responseTimeoutInMillis)
add in interface MemcachedCache<K,V>public boolean replace(K key, V value, int expirationInSecs, boolean noReply, long writeTimeoutInMillis, long responseTimeoutInMillis)
replace in interface MemcachedCache<K,V>public boolean cas(K key, V value, int expirationInSecs, long cas, boolean noReply, long writeTimeoutInMillis, long responseTimeoutInMillis)
cas in interface MemcachedCache<K,V>public Map<K,Boolean> casMulti(Map<K,ValueWithCas<V>> map, int expirationInSecs)
casMulti in interface MemcachedCache<K,V>public Map<K,Boolean> casMulti(Map<K,ValueWithCas<V>> map, int expirationInSecs, long writeTimeoutInMillis, long responseTimeoutInMillis)
casMulti in interface MemcachedCache<K,V>public boolean append(K key, V value, boolean noReply, long writeTimeoutInMillis, long responseTimeoutInMillis)
append in interface MemcachedCache<K,V>public boolean prepend(K key, V value, boolean noReply, long writeTimeoutInMillis, long responseTimeoutInMillis)
prepend in interface MemcachedCache<K,V>public Map<K,V> getMulti(Set<K> keys)
getMulti in interface MemcachedCache<K,V>public Map<K,V> getMulti(Set<K> keys, long writeTimeoutInMillis, long responseTimeoutInMillis)
getMulti in interface MemcachedCache<K,V>public V get(K key, boolean noReply, long writeTimeoutInMillis, long responseTimeoutInMillis)
get in interface MemcachedCache<K,V>public ValueWithKey<K,V> getKey(K key, boolean noReply)
public ValueWithKey<K,V> getKey(K key, boolean noReply, long writeTimeoutInMillis, long responseTimeoutInMillis)
getKey in interface MemcachedCache<K,V>public ValueWithCas<V> gets(K key, boolean noReply)
public ValueWithCas<V> gets(K key, boolean noReply, long writeTimeoutInMillis, long responseTimeoutInMillis)
gets in interface MemcachedCache<K,V>public Map<K,ValueWithCas<V>> getsMulti(Set<K> keys)
getsMulti in interface MemcachedCache<K,V>public Map<K,ValueWithCas<V>> getsMulti(Set<K> keys, long writeTimeoutInMillis, long responseTimeoutInMillis)
getsMulti in interface MemcachedCache<K,V>public V gat(K key, int expirationInSecs, boolean noReply, long writeTimeoutInMillis, long responseTimeoutInMillis)
gat in interface MemcachedCache<K,V>public boolean delete(K key, boolean noReply)
public boolean delete(K key, boolean noReply, long writeTimeoutInMillis, long responseTimeoutInMillis)
MemcachedCachedelete in interface MemcachedCache<K,V>key - item's keynoReply - whether you need to receive a reply or not. true means the quiet operation(no reply).writeTimeoutInMillis - write timeoutresponseTimeoutInMillis - response timeoutpublic Map<K,Boolean> deleteMulti(Set<K> keys)
deleteMulti in interface MemcachedCache<K,V>public Map<K,Boolean> deleteMulti(Set<K> keys, long writeTimeoutInMillis, long responseTimeoutInMillis)
deleteMulti in interface MemcachedCache<K,V>public long incr(K key, long delta, long initial, int expirationInSecs, boolean noReply)
public long incr(K key, long delta, long initial, int expirationInSecs, boolean noReply, long writeTimeoutInMillis, long responseTimeoutInMillis)
incr in interface MemcachedCache<K,V>public long decr(K key, long delta, long initial, int expirationInSecs, boolean noReply)
public long decr(K key, long delta, long initial, int expirationInSecs, boolean noReply, long writeTimeoutInMillis, long responseTimeoutInMillis)
decr in interface MemcachedCache<K,V>public String saslAuth(SocketAddress address, String mechanism, byte[] data)
public String saslAuth(SocketAddress address, String mechanism, byte[] data, long writeTimeoutInMillis, long responseTimeoutInMillis)
saslAuth in interface MemcachedCache<K,V>public String saslStep(SocketAddress address, String mechanism, byte[] data)
public String saslStep(SocketAddress address, String mechanism, byte[] data, long writeTimeoutInMillis, long responseTimeoutInMillis)
saslStep in interface MemcachedCache<K,V>public String saslList(SocketAddress address)
public String saslList(SocketAddress address, long writeTimeoutInMillis, long responseTimeoutInMillis)
saslList in interface MemcachedCache<K,V>public Map<String,String> stats(SocketAddress address)
public Map<String,String> stats(SocketAddress address, long writeTimeoutInMillis, long responseTimeoutInMillis)
stats in interface MemcachedCache<K,V>public Map<String,String> statsItems(SocketAddress address, String item)
statsItems in interface Commands<K,V>public Map<String,String> statsItems(SocketAddress address, String item, long writeTimeoutInMillis, long responseTimeoutInMillis)
statsItems in interface MemcachedCache<K,V>public boolean quit(SocketAddress address, boolean noReply)
public boolean quit(SocketAddress address, boolean noReply, long writeTimeoutInMillis, long responseTimeoutInMillis)
quit in interface MemcachedCache<K,V>public boolean flushAll(SocketAddress address, int expirationInSecs, boolean noReply)
public boolean flushAll(SocketAddress address, int expirationInSecs, boolean noReply, long writeTimeoutInMillis, long responseTimeoutInMillis)
flushAll in interface MemcachedCache<K,V>public boolean touch(K key, int expirationInSecs)
public boolean touch(K key, int expirationInSecs, long writeTimeoutInMillis, long responseTimeoutInMillis)
touch in interface MemcachedCache<K,V>public boolean noop(SocketAddress address)
public boolean noop(SocketAddress address, long writeTimeoutInMillis, long responseTimeoutInMillis)
noop in interface MemcachedCache<K,V>public boolean verbosity(SocketAddress address, int verbosity)
public boolean verbosity(SocketAddress address, int verbosity, long writeTimeoutInMillis, long responseTimeoutInMillis)
verbosity in interface MemcachedCache<K,V>public String version(SocketAddress address)
public String version(SocketAddress address, long writeTimeoutInMillis, long responseTimeoutInMillis)
version in interface MemcachedCache<K,V>Copyright © 2015 Project Grizzly. All Rights Reserved.