Friday, July 13, 2012

Where is my cached object ? SpyMemcached client & Memcached example

A simple example which uses spymemcached client against to the memcache server.
Memcached test scenario-1

Install multiple memcache server on the different servers : and
Create a client using these hosts and cache some object with a timeout.
Then check whether is your object cached in server 1 or server 2 ??

Installation guide for ubuntu 
Download spymemcached from here .

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import net.spy.memcached.MemcachedClient;

public class TestScenario1 {

public static void main(String[] args) {
//compose all servers list
List<InetSocketAddress> allServers = new ArrayList<InetSocketAddress> ();
allServers.add(new InetSocketAddress("", 11211));//server1 installed on localhost
allServers.add(new InetSocketAddress("", 11211));// this is server 2 installed on another host

try {
//create a connection against to the all servers
final MemcachedClient memcacheClient2AllServers = createMemcacheClient(allServers);
//create an object and tell memcache to cache it on one of the servers which are given as a list
// now we dont know where the object is actually cached ?? In server 1 or in server 2
// so i will check them in parellel with different threads

//create a connection against to the fist server only
final MemcachedClient memcacheClient2FirstServer = createMemcacheClient(allServers.subList(0, 1));
//create a connection against to the fist second server only
final MemcachedClient memcacheClient2SecondServer = createMemcacheClient(allServers.subList(1, 2));

//Check the cache from all clients by different threads
Thread thread4AllServers = createMemCacheCheckThread(memcacheClient2AllServers,"[1 & 2]");
Thread thread4FirstServerOnly = createMemCacheCheckThread(memcacheClient2FirstServer,"[1]");
Thread thread4SecondServerOnly = createMemCacheCheckThread(memcacheClient2SecondServer,"[2]");



} catch (Exception e) {

private static MemcachedClient createMemcacheClient(List<InetSocketAddress> allServers) throws IOException {
final MemcachedClient memcacheClient2AllServers = new MemcachedClient(allServers); ;
return memcacheClient2AllServers;

private static void cacheAnObject(final MemcachedClient memcacheClient2AllServers) {
SomeObject someObject = new SomeObject();
someObject.someField = "serkan sunel";
memcacheClient2AllServers.set("someKey", 60, someObject);

private static Thread createMemCacheCheckThread(final MemcachedClient memcacheClient, final String threadName) {
Thread thread = new Thread(){
public void run() {
while (true) {
try {
} catch (InterruptedException e) {

private void checkCache() {

SomeObject myObject=null;
Future<Object> f=memcacheClient.asyncGet("someKey");
try {
myObject=(SomeObject) f.get(5, TimeUnit.SECONDS);
if(myObject != null){
System.out.println("Value from cached object :" + myObject.someField + " thread :{"+Thread.currentThread().getName()+"}");
System.out.println("Cache is sweeped ! : thread :{"+Thread.currentThread().getName()+"}");
} catch(TimeoutException 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.
   // Do other timeout related stuff
} catch (Exception e) {
return thread;


No comments:

Post a Comment