use of net.spy.memcached.MemcachedClient in project serverless by bluenimble.
the class MemCachedPlugin method createClients.
private void createClients(ApiSpace space) throws PluginRegistryException {
// create sessions
JsonObject msgFeature = Json.getObject(space.getFeatures(), feature);
if (msgFeature == null || msgFeature.isEmpty()) {
return;
}
Iterator<String> keys = msgFeature.keys();
while (keys.hasNext()) {
String key = keys.next();
JsonObject feature = Json.getObject(msgFeature, key);
if (!this.getName().equalsIgnoreCase(Json.getString(feature, ApiSpace.Features.Provider))) {
continue;
}
String sessionKey = createKey(key);
if (space.containsRecyclable(sessionKey)) {
continue;
}
JsonObject spec = Json.getObject(feature, ApiSpace.Features.Spec);
String[] nodes = Lang.split(Json.getString(spec, Spec.Cluster), Lang.COMMA);
if (nodes == null) {
continue;
}
final JsonObject oAuth = Json.getObject(spec, Spec.Auth);
if (oAuth == null || oAuth.isEmpty()) {
continue;
}
final String user = Json.getString(oAuth, Spec.User);
final String password = Json.getString(oAuth, Spec.Password);
AuthDescriptor ad = new AuthDescriptor(new String[] { "PLAIN" }, new PlainCallbackHandler(user, password));
try {
MemcachedClient client = new MemcachedClient(new ConnectionFactoryBuilder().setProtocol(ConnectionFactoryBuilder.Protocol.BINARY).setAuthDescriptor(ad).build(), AddrUtil.getAddresses(Arrays.asList(nodes)));
space.addRecyclable(sessionKey, new RecyclableCacheClient(client));
} catch (IOException e) {
throw new PluginRegistryException(e.getMessage(), e);
}
}
}
use of net.spy.memcached.MemcachedClient in project zm-mailbox by Zimbra.
the class ZimbraMemcachedClient method remove.
/**
* Removes the value for given key.
* @param key
* @param timeout in millis
* @param waitForAck if true, block until ack'd or timeout; if false, return immediately
* @return
*/
public boolean remove(String key, long timeout, boolean waitForAck) {
Boolean success = null;
MemcachedClient client;
synchronized (this) {
client = mMCDClient;
if (timeout == DEFAULT_TIMEOUT)
timeout = mDefaultTimeout;
}
if (client == null)
return false;
Future<Boolean> future = client.delete(key);
if (waitForAck) {
try {
success = future.get(timeout, TimeUnit.MILLISECONDS);
} catch (TimeoutException e) {
ZimbraLog.misc.warn("memcached delete timed out after " + timeout + "ms", e);
future.cancel(false);
} catch (InterruptedException e) {
ZimbraLog.misc.warn("InterruptedException during memcached delete operation", e);
} catch (ExecutionException e) {
ZimbraLog.misc.warn("ExecutionException during memcached delete operation", e);
}
return success != null && success.booleanValue();
} else {
return true;
}
}
use of net.spy.memcached.MemcachedClient in project zm-mailbox by Zimbra.
the class ZimbraMemcachedClient method disconnect.
/**
* Shutdown the memcached client. Drain the queue and do a normal shutdown if possible.
* Both drain and normal shutdown are attempted with timeout. When unsuccessful, shutdown
* the client immediately.
* @param timeout in millis
*/
public void disconnect(long timeout) {
MemcachedClient client;
synchronized (this) {
client = mMCDClient;
mMCDClient = null;
if (timeout == DEFAULT_TIMEOUT)
timeout = mDefaultTimeout;
}
if (client != null) {
disconnect(client, timeout);
}
}
use of net.spy.memcached.MemcachedClient in project zm-mailbox by Zimbra.
the class ZimbraMemcachedClient method getBigByteArray.
/**
* Retrieves the big byte array value corresponding to the given key. See putBigByteArray method
* for more information.
* @param key
* @param timeout in millis
* @return null if no value is found for the key
*/
public byte[] getBigByteArray(String key, long timeout) {
MemcachedClient client;
synchronized (this) {
client = mMCDClient;
if (timeout == DEFAULT_TIMEOUT)
timeout = mDefaultTimeout;
}
if (client == null)
return null;
// Get the main value. This may be the entire value or the table of contents.
ByteArrayTranscoder bat = new ByteArrayTranscoder();
ByteArray mainValue = null;
Future<ByteArray> future = client.asyncGet(key, bat);
try {
mainValue = future.get(timeout, TimeUnit.MILLISECONDS);
} catch (TimeoutException e) {
ZimbraLog.misc.warn("memcached asyncGetBulk timed out after " + timeout + "ms", e);
future.cancel(false);
} catch (InterruptedException e) {
ZimbraLog.misc.warn("InterruptedException during memcached asyncGetBulk operation", e);
} catch (ExecutionException e) {
ZimbraLog.misc.warn("ExecutionException during memcached asyncGetBulk operation", e);
}
if (mainValue == null)
return null;
byte[] prefixed = mainValue.getBytes();
if (prefixed == null || prefixed.length < 2)
return null;
// First byte is value/TOC indicator. Remove it.
byte[] value = new byte[prefixed.length - 1];
System.arraycopy(prefixed, 0, value, 0, prefixed.length - 1);
// If it's a short value, just return it.
if (prefixed[prefixed.length - 1] == BBA_PREFIX_VALUE)
return value;
// We have a table of contents.
String tocEncoded = null;
ByteArrayChunksTOC toc;
try {
tocEncoded = new String(value, "utf-8");
toc = new ByteArrayChunksTOC(tocEncoded);
} catch (UnsupportedEncodingException e) {
ZimbraLog.misc.warn("Unable to decode BBA table of contents", e);
return null;
} catch (ServiceException e) {
ZimbraLog.misc.warn("Invalid big byte array TOC: " + tocEncoded);
return null;
}
int numChunks = toc.getNumChunks();
if (numChunks <= 0) {
// This should never happen. Just a sanity check.
ZimbraLog.misc.warn("Big byte array TOC has numChunks=0");
return null;
}
List<String> chunkKeys = new ArrayList<String>(numChunks);
for (int i = 0; i < numChunks; ++i) {
String ck = key + ":" + toc.getFingerprint() + "." + i;
chunkKeys.add(ck);
}
// Get the chunks from memcached.
Map<String, ByteArray> vals = null;
Future<Map<String, ByteArray>> futureChunks = client.asyncGetBulk(chunkKeys, bat);
try {
vals = futureChunks.get(timeout, TimeUnit.MILLISECONDS);
} catch (TimeoutException e) {
ZimbraLog.misc.warn("memcached asyncGetBulk timed out after " + timeout + "ms", e);
futureChunks.cancel(false);
} catch (InterruptedException e) {
ZimbraLog.misc.warn("InterruptedException during memcached asyncGetBulk operation", e);
} catch (ExecutionException e) {
ZimbraLog.misc.warn("ExecutionException during memcached asyncGetBulk operation", e);
}
// Make sure all chunks are there. If not, just return null.
if (vals == null)
return null;
ByteArray[] byteArrays = new ByteArray[numChunks];
int index = 0;
for (String ck : chunkKeys) {
ByteArray ba = (ByteArray) vals.get(ck);
if (ba == null)
return null;
byteArrays[index] = ba;
++index;
}
byte[] data = null;
try {
data = ByteArrayChunks.combine(byteArrays, toc);
} catch (ServiceException e) {
ZimbraLog.misc.warn("Unable to reassemble byte array from chunks", e);
}
return data;
}
use of net.spy.memcached.MemcachedClient in project zm-mailbox by Zimbra.
the class ZimbraMemcachedClient method put.
/**
* Puts the key/value pair.
* @param key
* @param value
* @param expirySec expiry in seconds
* @param timeout in millis
* @param waitForAck if true, block until ack'd or timeout; if false, return immediately
* @return
*/
public boolean put(String key, Object value, int expirySec, long timeout, boolean waitForAck) {
MemcachedClient client;
synchronized (this) {
client = mMCDClient;
if (expirySec == DEFAULT_EXPIRY)
expirySec = mDefaultExpiry;
if (timeout == DEFAULT_TIMEOUT)
timeout = mDefaultTimeout;
}
if (client == null)
return false;
Future<Boolean> future = client.set(key, expirySec, value);
if (waitForAck) {
Boolean success = null;
try {
success = future.get(timeout, TimeUnit.MILLISECONDS);
} catch (TimeoutException e) {
ZimbraLog.misc.warn("memcached set timed out after " + timeout + "ms", e);
future.cancel(false);
} catch (InterruptedException e) {
ZimbraLog.misc.warn("InterruptedException during memcached set operation", e);
} catch (ExecutionException e) {
ZimbraLog.misc.warn("ExecutionException during memcached set operation", e);
}
return success != null && success.booleanValue();
} else {
return true;
}
}
Aggregations