Getting started with Memcached....
Memcached is an opensource distributed in memory caching system.It is very simple, powerful, relaible and easy to use.Mainly used for speeding up dynamic web applications by reducing database load."Memcahed is used by several very large, well-known sites including LiveJournal,Wikipedia,Flickr,Bebo,Twitter,Typepad,Yellowbot,Youtube,Digg,Wordpress,Craigslist,Mixi" -memcached.org
General caching logic...
Object obj = cache.get(key); //Checks if available in cache before invoking db.
if(obj != null) {
return obj; // returns the value got from cache without invoking the db
}
//Not present in cache, invoke db
obj = invokeDB();
if(obj! = null) {
cache.put(key, obj) //Stores it in cache
}
Memcached clients are available for most popular languages.Here is a quick introduction on using memcached with Java.The memcached Java client used is netspy version 2.4, Java version : JDK 1.5, Memcached Version : Memcached 1.2.1
Download memcached and unzip to a location. Start the memcached server by clicking memcached.exe http://memcached.org/
Tip : If you are running multiple instance of memcached, here is how the connection url looks like and rest all remain same.
String url = "hostName1:11211 hostName2:11211 hostName3:11211";
Here is a sample code in Java for memcached.Both memcached-2.1.jar and spy-2.4.jar are required in the classpath .
import java.net.InetSocketAddress;
import java.util.Map;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import net.spy.memcached.AddrUtil;
import net.spy.memcached.MemcachedClient;
public class MemcachedTest {
private static MemcachedClient client = null;
private static long lookupTimeout = 3;
private static final String hostName ="UOPONLC6MBDB1";
private static final String portNum = "11211"; // Default Memcached port
public static void main(String a[]) {
String key = "mykey";
String value = "myValue";
connect();
System.out.println("Calling Put.....");
put(key, value);
printStatus();
System.out.println("Value from Get : " + get(key));
System.out.println("Calling Delete.....");
delete(key);
printStatus();
System.out.println("Calling Put.....");
put(key, value);
printStatus();
System.out.println("Calling Flusg.....");
flush();
printStatus();
disconnect();
}
public static void printStatus() {
Map memcachedStatsMap = getMemcachedStats();
System.out.println("\n\n/********** Mecached Status - Start
******************/");
System.out.println("No. of Item in Cache : " +
memcachedStatsMap.get("curr_items"));
System.out.println("No of Connections : "+
memcachedStatsMap.get("curr_connections"));
System.out.println("Bytes used : " + memcachedStatsMap.get("bytes"));
System.out.println("/********** Mecached Status - End ******************/\n\n");
}
private static void connect() {
final String url = hostName + ":" + portNum;
try {
client = new MemcachedClient(AddrUtil.getAddresses(url));
} catch(Exception e) {
//Handle Exception
}
}
private static void disconnect() {
try {
client.shutdown();
} catch(Exception e) {
//Handle Exception
}
}
public static Object get(String key) {
Object value = null;
Future f = client.asyncGet(key);
try {
value = f.get(lookupTimeout, TimeUnit.SECONDS);
} catch (Exception e) {
f.cancel(true);
//Handle Exception
}
return value;
}
public static Boolean put(String key, Object obj) {
Boolean result = false; int timeOut = 1 * 60 * 60; //time out
Future f = client.set(key, timeOut, obj);
try {
result = f.get(lookupTimeout, TimeUnit.SECONDS);
} catch (Exception e) {
f.cancel(true);
//Handle Exception
}
return result;
}
/**
* Deletes a given key from the Memcached
*/
public static Boolean delete(final String key) {
Boolean result = false;
Future f = client.delete(key);
try {
result = f.get(lookupTimeout, TimeUnit.SECONDS);
} catch (Exception e){
f.cancel(true);
//Handle Exception
}
return result;
}
public static void flush() {
try {
client.flush();
} catch (Exception e) {
//Handle Exception
}
}
public static Map getMemcachedStats() {
Map thisClientMap = null;
InetSocketAddress inetSocketAddress = new InetSocketAddress(hostName,
Integer.parseInt(portNum));
try {
thisClientMap = client.getStats().get(inetSocketAddress);
} catch (Exception e) {
//Handle Exception
}
return thisClientMap;
}
}
Memcached is an opensource distributed in memory caching system.It is very simple, powerful, relaible and easy to use.Mainly used for speeding up dynamic web applications by reducing database load."Memcahed is used by several very large, well-known sites including LiveJournal,Wikipedia,Flickr,Bebo,Twitter,Typepad,Yellowbot,Youtube,Digg,Wordpress,Craigslist,Mixi" -memcached.org
General caching logic...
Object obj = cache.get(key); //Checks if available in cache before invoking db.
if(obj != null) {
return obj; // returns the value got from cache without invoking the db
}
//Not present in cache, invoke db
obj = invokeDB();
if(obj! = null) {
cache.put(key, obj) //Stores it in cache
}
Memcached clients are available for most popular languages.Here is a quick introduction on using memcached with Java.The memcached Java client used is netspy version 2.4, Java version : JDK 1.5, Memcached Version : Memcached 1.2.1
Download memcached and unzip to a location. Start the memcached server by clicking memcached.exe http://memcached.org/
Tip : If you are running multiple instance of memcached, here is how the connection url looks like and rest all remain same.
String url = "hostName1:11211 hostName2:11211 hostName3:11211";
Here is a sample code in Java for memcached.Both memcached-2.1.jar and spy-2.4.jar are required in the classpath .
import java.net.InetSocketAddress;
import java.util.Map;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import net.spy.memcached.AddrUtil;
import net.spy.memcached.MemcachedClient;
public class MemcachedTest {
private static MemcachedClient client = null;
private static long lookupTimeout = 3;
private static final String hostName ="UOPONLC6MBDB1";
private static final String portNum = "11211"; // Default Memcached port
public static void main(String a[]) {
String key = "mykey";
String value = "myValue";
connect();
System.out.println("Calling Put.....");
put(key, value);
printStatus();
System.out.println("Value from Get : " + get(key));
System.out.println("Calling Delete.....");
delete(key);
printStatus();
System.out.println("Calling Put.....");
put(key, value);
printStatus();
System.out.println("Calling Flusg.....");
flush();
printStatus();
disconnect();
}
public static void printStatus() {
Map memcachedStatsMap = getMemcachedStats();
System.out.println("\n\n/********** Mecached Status - Start
******************/");
System.out.println("No. of Item in Cache : " +
memcachedStatsMap.get("curr_items"));
System.out.println("No of Connections : "+
memcachedStatsMap.get("curr_connections"));
System.out.println("Bytes used : " + memcachedStatsMap.get("bytes"));
System.out.println("/********** Mecached Status - End ******************/\n\n");
}
private static void connect() {
final String url = hostName + ":" + portNum;
try {
client = new MemcachedClient(AddrUtil.getAddresses(url));
} catch(Exception e) {
//Handle Exception
}
}
private static void disconnect() {
try {
client.shutdown();
} catch(Exception e) {
//Handle Exception
}
}
public static Object get(String key) {
Object value = null;
Future f = client.asyncGet(key);
try {
value = f.get(lookupTimeout, TimeUnit.SECONDS);
} catch (Exception e) {
f.cancel(true);
//Handle Exception
}
return value;
}
public static Boolean put(String key, Object obj) {
Boolean result = false; int timeOut = 1 * 60 * 60; //time out
Future f = client.set(key, timeOut, obj);
try {
result = f.get(lookupTimeout, TimeUnit.SECONDS);
} catch (Exception e) {
f.cancel(true);
//Handle Exception
}
return result;
}
/**
* Deletes a given key from the Memcached
*/
public static Boolean delete(final String key) {
Boolean result = false;
Future f = client.delete(key);
try {
result = f.get(lookupTimeout, TimeUnit.SECONDS);
} catch (Exception e){
f.cancel(true);
//Handle Exception
}
return result;
}
public static void flush() {
try {
client.flush();
} catch (Exception e) {
//Handle Exception
}
}
public static Map getMemcachedStats() {
Map thisClientMap = null;
InetSocketAddress inetSocketAddress = new InetSocketAddress(hostName,
Integer.parseInt(portNum));
try {
thisClientMap = client.getStats().get(inetSocketAddress);
} catch (Exception e) {
//Handle Exception
}
return thisClientMap;
}
}