How to increment/decrement values in memcached

Memcached is one of the most popular caching engine. I had an requirement in which I wanted to increment a particular key by some value. Initially i issued get request to get the value. If it was null i would add the key else I will set it by the new value. But with this design I was making two calls, one get and one set which became expensive once number of operations increased.

So I explored and found incr/decr methods. I used the default method with 2 parameters. In this case if key did not exist, it was setting it as null.On further drilling I found one more overloaded method in which we can set the default value of the key if it does not exist. Overall this helped be in improving my performance by almost 100%

client.incr("key", 1,1);
client.decr("key", 1,1);

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 *