I’ve been wanting to have a look at memcached for a while. A lot of people swear by it for distributed caching, and it’s now part of Google App Engine’s infrastructure, so I figure it was worth taking a look what’s doable.
If you’re coming from a Java background, you’re probably familiar with ehcache or some similar caching library - basically a key/value store of elements which timeout after a certain period.
Memcache provides a similar capability, but out of process to your app server. You run memcached on your server (as a standalone process), and your clients communicate with it over sockets. This makes it way slower than an in-process solution, but it does give you some resilience to app server crashes and whatnot. Also means you can run central caching infrastructure that all of you clients talk to - or even take advantage of memcache’s distributed caching infrastructure.
memcache is available on all the major platforms. I just installed it via macports (port install memcached
) then kicked it off in daemon mode with the defaults via memcached -d
.
With the daemon up and running, it was time to explore some memcached java clients APIs. The one I settled on was the spymemcached approach. Super simple API, and some great samples to get you up and running.
Getting things into and out of the cache from groovy looks something like this:
import net.spy.memcached.* // constructor takes the host and the port of your memcached server // (or an array if you have a cluster for failover) MemcachedClient c=new MemcachedClient( new InetSocketAddress("localhost", 11211)) // "add" only puts it in the store if the key doesn't exist c.add("sample", 10, "test"); // "set" forces the value in regardless of the store c.set("mapper", 20, [ a: 10, b: 20 ]) // Retrieve a simple value (synchronously). println c.get("sample"); // But complex objects work great too c.get("mapper").each { println it }; // You can delete stuff too c.delete("mapper") // And get some stats off the cache server println c.getStats()
I’m using the cache to hold thumbnail data for groovyblogs.org - which saves sticking anything in a database (but is still safe for restarting the appserver on new version installs).
As for Grails integration, you probably want to create a simple Spring FactoryBean so you can easily inject the MemcachedClient wherever you need it. I put together something like this:
import org.springframework.beans.factory.config.AbstractFactoryBean import net.spy.memcached.* /** * A simple factory bean for Memcache Clients. * * @author Glen Smith */ public class MemcacheFactoryBean extends AbstractFactoryBean { String host int port public Object createInstance() { return new MemcachedClient( new InetSocketAddress(host, port)) } public Class getObjectType() { return MemcachedClient.class } }
To consume the new factory, add an entry to your grails-app/conf/resources.groovy to declare the new bean:
beans = { memCache(MemcacheFactoryBean) { host = "localhost" port = 11211 } }
Then just need a def memCache
wherever you want to consume things in your grails app and enjoy the injection goodness!
Happy caching!
(I’m supposed to be working on finishing the course notes for my Canberra Maven Quickstart course…. perhaps I’ll make an exercise out of all this!)