Wednesday 24 August 2016

Spymemcached: Setting time out for Memcached request

Some times Memcached may delay the processing. In some situations like Memcached serve is down (or) disabled MemcachedConnection will continue to attempt to reconnect and replay pending operations until it comes back up. To prevent this from causing your application to hang, you can use one of the asynchronous mechanisms to time out a request and cancel the operation to the server.

Future<Object> future = memClient.asyncGet("E2345");

try {
         String data = (String) future.get(5, TimeUnit.SECONDS);
         System.out.println("Data is " + data);
} catch (Exception 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. It is okay to cancel it if running.
         future.cancel(true);
}


Above statement waits maximum of 5 seconds to get reply, if it is unable to get the reply, it cancel the task.
package com.sampe.cache;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import net.spy.memcached.MemcachedClient;

/**
 * Utility class to create client.
 * 
 * @author harikrishna_gurram
 *
 */
public class CacheClientUtil {

 private static MemcachedClient client = null;
 private static Logger logger = LogManager.getLogger();

 public static Optional<MemcachedClient> getClient(InetSocketAddress addr) {
  if (Objects.isNull(addr)) {
   logger.error("getClient: addr shouldn't be null");
   return Optional.empty();
  }

  try {
   client = new MemcachedClient(addr);
  } catch (IOException e) {
   logger.error(e);
   return Optional.empty();
  }

  return Optional.of(client);
 }

 public static Optional<MemcachedClient> getClient(List<InetSocketAddress> addrs) {
  if (Objects.isNull(addrs)) {
   logger.error("getClient: addrs shouldn't be null");
   return Optional.empty();
  }

  try {
   client = new MemcachedClient(addrs);
  } catch (IOException e) {
   logger.error(e);
   return Optional.empty();
  }

  return Optional.of(client);
 }

 public static void shutdownClient(MemcachedClient client) {
  if (Objects.isNull(client)) {
   logger.error("shutdownClient: client object is already closed");
   return;
  }

  client.shutdown();
 }
}

package com.sampe.cache;

import java.net.InetSocketAddress;
import java.util.Optional;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import net.spy.memcached.MemcachedClient;

public class Test {
 private static Logger logger = LogManager.getLogger();

 public static void main(String args[]) throws InterruptedException, ExecutionException {
  /* Get MemcachedClient */
  InetSocketAddress address = new InetSocketAddress("127.0.0.1", 11211);

  Optional<MemcachedClient> client = CacheClientUtil.getClient(address);

  if (!client.isPresent()) {
   logger.error("Unable to create client instance");
   return;
  }

  MemcachedClient memClient = client.get();

  memClient.set("E2345", 900, "Hari Krishna Gurram");

  Future<Object> future = memClient.asyncGet("E2345");

  try {
   String data = (String) future.get(5, TimeUnit.SECONDS);
   System.out.println("Data is " + data);
  } catch (Exception 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. It is okay to cancel it if running.
   future.cancel(true);
  }

  CacheClientUtil.shutdownClient(memClient);

 }
}


Output
Data is Hari Krishna Gurram




Previous                                                 Next                                                 Home

No comments:

Post a Comment