Search in sources :

Example 51 with MemcachedClient

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;
}
Also used : HashMap(java.util.HashMap) MemcachedClient(net.spy.memcached.MemcachedClient) ExecutionException(java.util.concurrent.ExecutionException) HashMap(java.util.HashMap) Map(java.util.Map) TimeoutException(java.util.concurrent.TimeoutException)

Example 52 with MemcachedClient

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();
    }
}
Also used : MemcachedClient(net.spy.memcached.MemcachedClient) IgniteAtomicLong(org.apache.ignite.IgniteAtomicLong) Ignite(org.apache.ignite.Ignite)

Aggregations

MemcachedClient (net.spy.memcached.MemcachedClient)52 Test (org.junit.Test)22 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)20 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)17 InetSocketAddress (java.net.InetSocketAddress)11 ExecutionException (java.util.concurrent.ExecutionException)9 ConnectionFactoryBuilder (net.spy.memcached.ConnectionFactoryBuilder)8 ConnectionFactory (net.spy.memcached.ConnectionFactory)7 IOException (java.io.IOException)6 TimeoutException (java.util.concurrent.TimeoutException)6 Map (java.util.Map)5 HashMap (java.util.HashMap)4 ArrayList (java.util.ArrayList)3 Properties (java.util.Properties)3 BinaryConnectionFactory (net.spy.memcached.BinaryConnectionFactory)3 Predicate (com.google.common.base.Predicate)2 Supplier (com.google.common.base.Supplier)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 Config (com.hazelcast.config.Config)2