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 : 127.0.0.1 and 172.28.138.184.
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.io.IOException;
import java.net.InetSocketAddress;
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) {
@SuppressWarnings("unchecked")
//compose all servers list
List<InetSocketAddress> allServers = new ArrayList<InetSocketAddress> ();
allServers.add(new InetSocketAddress("127.0.0.1", 11211));//server1 installed on localhost
allServers.add(new InetSocketAddress("172.28.138.184", 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
cacheAnObject(memcacheClient2AllServers);
// 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]");

thread4AllServers.start();
thread4FirstServerOnly.start();
thread4SecondServerOnly.start();

thread4AllServers.join();
thread4FirstServerOnly.join();
thread4SecondServerOnly.join();

} catch (Exception e) {
e.printStackTrace();
}
}

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(){
@Override
public void run() {
while (true) {
try {
checkCache();
sleep(1000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

}
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()+"}");
}else{
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.
   f.cancel(false);
   // Do other timeout related stuff
} catch (Exception e) {
e.printStackTrace();
}
}
};
thread.setName(threadName);
return thread;
}

}

No comments:

Post a Comment