Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Spymemcached: Increment and Decrement the value associated with key

MemcachedClient class provides following methods to increment and decrement the values associated with given key.

public long incr(String key, int by);
public long decr(String key, int by)


Following is the complete working 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 OptionalMemcachedClient> 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 OptionalMemcachedClient> getClient(ListInetSocketAddress> 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);

OptionalMemcachedClient> client = CacheClientUtil.getClient(address);

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

MemcachedClient memClient = client.get();

FutureBoolean> future = memClient.set("krishna", 900, "25000");

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

System.out.println("Value of the Key Krishna is " + memClient.get("krishna"));

System.out.println("\nIncrement the value associated with key krishna by 10000");
memClient.incr("krishna", 10000);
System.out.println("Value of the key krishna is " + memClient.get("krishna"));

System.out.println("\nDecrement the value associated with key krishna by 5000");
memClient.decr("krishna", 5000);
System.out.println("Value of the key krishna is " + memClient.get("krishna"));

CacheClientUtil.shutdownClient(memClient);

}
}


Output
set status:true
Value of the key krishna is 25000

Increment the value associated with key krishna by 10000
Value of the key krishna is 35000

Decrement the value associated with key krishna by 5000
Value of the key krishna is 30000



Previous                                                 Next                                                 Home


This post first appeared on Java Tutorial : Blog To Learn Java Programming, please read the originial post: here

Share the post

Spymemcached: Increment and Decrement the value associated with key

×

Subscribe to Java Tutorial : Blog To Learn Java Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×