6/12/12

Memcache

Hi All,

I came across a interesting caching mechanism, it is quite simple to use and understand. Spymemcached is used for this purpose.  You can refer the site http://sacharya.com/using-memcached-with-java/ for full details.

To work with it you will require a memcache s/w and a couple of library files.

Memcache server can be downloaded from : http://memcached.org/

Libraries(jars) :  http://code.google.com/p/spymemcached/downloads/detail?name=memcached-2.6rc1.jar&can=4&q=

There are many method's in the package but we will be mainly using three methods only and they are set(key,expirytime,value),get(key),delete(key).

A sample java code snippet to understand memcache usage is as follows.

void whatIsMemcache() {
        try {
            //127.0.0.1 - machine's ip
            //11211 - port in which memcache server running
            MemcachedClient c = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211));
            String s = "Raghu";
            c.set("someKey", 30, s);

            Object myObject = c.get("someKey");
            System.out.println("myobject---->" + myObject.toString());
            c.delete("someKey");

        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

No comments:

Post a Comment

Popular Posts