18
2009
A first taste of Memcache
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!)
1 Comment + Add Comment
Leave a comment
Glen Smith
Archives
- January 2012
- November 2011
- October 2011
- September 2011
- August 2011
- July 2011
- June 2011
- April 2011
- March 2011
- January 2011
- November 2010
- October 2010
- September 2010
- August 2010
- July 2010
- June 2010
- May 2010
- April 2010
- February 2010
- January 2010
- December 2009
- November 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
- June 2007
- May 2007
- April 2007
- March 2007
- February 2007
- January 2007
- December 2006
- November 2006
- October 2006
- September 2006
- August 2006
- July 2006
- June 2006
- May 2006
- April 2006
- March 2006
- February 2006
- January 2006
- December 2005
- November 2005
- October 2005
- September 2005
- August 2005
- July 2005
- June 2005
- May 2005
- April 2005
- March 2005
- February 2005
- January 2005
- December 2004
- November 2004
- October 2004
- September 2004
- August 2004
- July 2004
- June 2004
- May 2004
- March 2004
- February 2004
- January 2004
- December 2003
- November 2003
- October 2003
- September 2003

An article by Glen





thank you glen for your interesting blog-posts. one remark on
maven: take a look at gradle too. IMO gradle is a much much
better buildsystem esp. compared to maven.
we started to use gradle in the enterprise and after a few month
developers and buildmanagers love it.