Wednesday, 24 August 2016

Spymemcached: Get server statistics


MemcachedClient class provides getStats method, which returns the statistics of Memcahed server (or) servers.
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 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();

  System.out.println("Memcached Statistics - " + memClient.getStats());

  CacheClientUtil.shutdownClient(memClient);

 }
}


Output
Memcached Statistics - {/127.0.0.1:11211={cmd_touch=0, incr_hits=2, evictions=0, touch_hits=0, expired_unfetched=3, pid=873, cas_badval=1, cmd_flush=0, total_items=53, cas_hits=3, accepting_conns=1, auth_errors=0, reserved_fds=20, crawler_items_checked=0, conn_yields=0, version=1.4.24, listen_disabled_num=0, get_misses=4, hash_is_expanding=0, touch_misses=0, auth_cmds=0, cas_misses=0, delete_misses=1, get_hits=66, malloc_fails=0, delete_hits=2, curr_connections=11, bytes_written=8065, hash_bytes=524288, libevent=2.0.22-stable, lrutail_reflocked=0, crawler_reclaimed=0, decr_hits=3, limit_maxbytes=67108864, decr_misses=0, reclaimed=21, cmd_get=70, hash_power_level=16, curr_items=1, threads=4, cmd_set=68, bytes_read=4739, uptime=46972, total_connections=53, incr_misses=0, connection_structures=12, bytes=77, evicted_unfetched=0, rusage_system=0.491020, time=1468166962, pointer_size=64, rusage_user=0.306486}}



Previous                                                 Next                                                 Home

No comments:

Post a Comment