use of net.spy.memcached.MemcachedClient in project zm-mailbox by Zimbra.
the class ZimbraMemcachedClient method getMulti.
/**
* Retrieves the values corresponding to the given keys.
* The returned map is never null and always contains an entry for every key.
* Null value is used for any key not found in memcached.
* @param keys
* @param timeout in millis
* @return map of (key, value); missing keys have null value
*/
public Map<String, Object> getMulti(Collection<String> keys, long timeout) {
Map<String, Object> value = null;
MemcachedClient client;
synchronized (this) {
client = mMCDClient;
if (timeout == DEFAULT_TIMEOUT)
timeout = mDefaultTimeout;
}
if (client != null) {
Future<Map<String, Object>> future = client.asyncGetBulk(keys);
try {
value = 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);
}
}
// for any keys missing from memcached response.
if (value == null)
value = new HashMap<String, Object>(keys.size());
for (String key : keys) {
if (!value.containsKey(key))
value.put(key, null);
}
return value;
}
use of net.spy.memcached.MemcachedClient in project ignite by apache.
the class MemcacheRestExample method main.
/**
* @param args Command line arguments.
* @throws Exception In case of error.
*/
public static void main(String[] args) throws Exception {
MemcachedClient client = null;
try (Ignite ignite = Ignition.start(MemcacheRestExampleNodeStartup.configuration())) {
System.out.println();
System.out.println(">>> Memcache REST example started.");
IgniteCache<String, Object> cache = ignite.cache("default");
client = startMemcachedClient(host, port);
// Put string value to cache using Memcache binary protocol.
if (client.add("strKey", 0, "strVal").get())
System.out.println(">>> Successfully put string value using Memcache client.");
// Check that string value is actually in cache using traditional
// Ignite API and Memcache binary protocol.
System.out.println(">>> Getting value for 'strKey' using Ignite cache API: " + cache.get("strKey"));
System.out.println(">>> Getting value for 'strKey' using Memcache client: " + client.get("strKey"));
// Remove string value from cache using Memcache binary protocol.
if (client.delete("strKey").get())
System.out.println(">>> Successfully removed string value using Memcache client.");
// Check that cache is empty.
System.out.println(">>> Current cache size: " + cache.size() + " (expected: 0).");
// Put integer value to cache using Memcache binary protocol.
if (client.add("intKey", 0, 100).get())
System.out.println(">>> Successfully put integer value using Memcache client.");
// Check that integer value is actually in cache using traditional
// Ignite API and Memcache binary protocol.
System.out.println(">>> Getting value for 'intKey' using Ignite cache API: " + cache.get("intKey"));
System.out.println(">>> Getting value for 'intKey' using Memcache client: " + client.get("intKey"));
// Remove string value from cache using Memcache binary protocol.
if (client.delete("intKey").get())
System.out.println(">>> Successfully removed integer value using Memcache client.");
// Check that cache is empty.
System.out.println(">>> Current cache size: " + cache.size() + " (expected: 0).");
// Create atomic long and close it after test is done.
try (IgniteAtomicLong l = ignite.atomicLong("atomicLong", 10, true)) {
// Increment atomic long by 5 using Memcache client.
if (client.incr("atomicLong", 5, 0) == 15)
System.out.println(">>> Successfully incremented atomic long by 5.");
// Increment atomic long using Ignite API and check that value is correct.
System.out.println(">>> New atomic long value: " + l.incrementAndGet() + " (expected: 16).");
// Decrement atomic long by 3 using Memcache client.
if (client.decr("atomicLong", 3, 0) == 13)
System.out.println(">>> Successfully decremented atomic long by 3.");
// Decrement atomic long using Ignite API and check that value is correct.
System.out.println(">>> New atomic long value: " + l.decrementAndGet() + " (expected: 12).");
}
} finally {
if (client != null)
client.shutdown();
}
}
Aggregations