Installing and using Memcached (java client) in windows

Java caching frameworks like EHCache and OSCache are essentially HashMap objects in your application code. Whenever you add a new object to the cache it will be stored in the memory of your application. This strategy works fine for storing small amounts of data, but it doesn’t work for caching more than few gigabytes (GB). The designers of the Memcached server took a distributed architectural approach, which allows for system scalability. As a result, you can use Memcached to cache a huge amount of data.

Download windows binary of memcached from here Memcached for windows

I had downloaded 1.4.2 32 bit version.

Expand the folder and open command prompt with administrator’s privilege (Run as administrator)

Now run this command to install memcached :

memcached.exe -d install

Run this command to start memcached :

memcached.exe -d start

Run this command to stop memcached :

memcached.exe -d stop

Run this command to uninstall memcached :

memcached.exe -d uninstall

Once installed, you can even check your services(services.msc) to see if it has got registered there or not.

You can also test it by issuing telnet command:

telnet localhost 11210

You might be running on some other port

Now lets build a java program to use memcached. We will use this java client : Java client for memcached

Here is the sample code :

public class MemcachedClientExample {
 
	public static void main(String[] args) throws Exception{
		 if(args.length < 2){
	        System.out.println("Please specify command line options");
	        return;
	    }
	    String commandName = args[0];
 
	    MemcachedClient memcachedClient = new MemcachedClient(AddrUtil.getAddresses("127.0.0.1:11211"));
	    if(commandName.equals("get")){
	        String keyName= args[1];
	        System.out.println("Key Name " +keyName);
	        System.out.println("Value of key " +memcachedClient.get(keyName));
	    }else if(commandName.equals("set")){
	        String keyName =args[1];
	        String value=args[2];
	        System.out.println("Key Name " +keyName + " value=" + value);
	        Future<Boolean> result= memcachedClient.set(keyName, 0, value);
	        System.out.println("Result of set " + result.get());
	    }else if(commandName.equals("add")){
	        String keyName =args[1];
	        String value=args[2];
	        System.out.println("Key Name " +keyName + " value=" + value);
	        Future<Boolean> result= memcachedClient.add(keyName, 0, value);
	        System.out.println("Result of add " + result.get());
	    }else if(commandName.equals("replace")){
	        String keyName =args[1];
	        String value=args[2];
	        System.out.println("Key Name " +keyName + " value=" + value);
	        Future<Boolean> result= memcachedClient.replace(keyName, 0, value);
	        System.out.println("Result of replace " + result.get());
	    }else if(commandName.equals("delete")){
	        String keyName =args[1];
	        System.out.println("Key Name " +keyName );
	        Future<Boolean> result= memcachedClient.delete(keyName);
	        System.out.println("Result of delete " + result.get());
	    }else{
	        System.out.println("Command not found");
	    }
	    memcachedClient.shutdown();
 
	}

Mar Java Mit Java 🙂

Uday Ogra

Connect with me at http://facebook.com/tendulkarogra and lets have some healthy discussion :)

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *