Wednesday, 24 August 2016

Spymemcached: gets: Get CAS token associated with the data

‘gets’ command is used to get the value with CAS token. CAS stands for check and set. CAS token is very useful in concurrent applications. For example, you want to deduct some amount from customer account on every transaction.

Thread 1 reads cust1 account balance as 1500
Thread 2 also reads the cust1 account balance as 1500
Thread 2 deduct 300 from cust1 account = 1500-300 = 1200
Thread 1 deduct 100 from cust1 account = 1500-100 = 1400.

As you see above flow, Actual balance to be deducted from cust1 account is 400, but final amount is 1400. To handle these kind of scenarios, we should use CAS command. I will explain about CAS command in next post, let us concentrate on gets command here.

Syntax
gets key

You can also get the values associated with multiple keys at a time.

Syntax
gets key1 key2….keyN
  

Following is the complete working Java application.
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 org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import net.spy.memcached.CASValue;
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();

    /* Set some data */
    String data = "Tutorial";

    Future<Boolean> future = memClient.set("12345", 900, data);

    System.out.println("set status:" + future.get());

    System.out.println("Value of the key 12345 is " + memClient.get("12345"));

    CASValue casValue = memClient.gets("12345");

    System.out.println("cas value is " + casValue.getCas());

    System.out.println("\nUpdating the value associated with key 12345\n");
    memClient.set("12345", 900, "java");

    System.out.println("Value of the key 12345 is " + memClient.get("12345"));

    casValue = memClient.gets("12345");

    System.out.println("cas value is " + casValue.getCas());

    CacheClientUtil.shutdownClient(memClient);

  }
}


Output
set status:true
Value of the key 12345 is Tutorial
cas value is 33

Updating the value associated with key 12345

Value of the key 12345 is java
cas value is 34



Previous                                                 Next                                                 Home

No comments:

Post a Comment