Following step-by-step procedure explains how to append data
to the value associated with a key in
the cache.
Step 1: Get
MemcachedClient instance. It is a client to a memcached server. Following
statement is used to define MemcachedClient.
MemcachedClient client
= new MemcachedClient(new InetSocketAddress(hostname, portNum));
Step 2: Once
you got the MemcachedClient instance, you can able to write data to server and
read data from server. Spymemcached provides following method to prepend data.
public OperationFuture<Boolean>
prepend(String key, Object val)
Prepend to an existing
value in the cache.
Step 3: After
that, you can call the get method of MemcachedClient instance to get the data
associated with given key.
public Object get(String key)
Above method return an
object associated with given key. If the key don’t exist in memcached server,
then it return null.
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 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.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")); /* Appending data */ future = memClient.prepend("12345", "Java "); System.out.println("status:" + future.get()); System.out.println("Value of the key 12345 is " + memClient.get("12345")); CacheClientUtil.shutdownClient(memClient); } }
Output
set status:true Value of the key 12345 is Tutorial status:true Value of the key 12345 is Java Tutorial
No comments:
Post a Comment