Archive for September, 2009

I’m a big fan of using memcached for improving the scalability of websites so when I recently fell in love with the Grails Framework I began to look for a way to configure the memcached client which blends in with the framework. Luckily Grails provides a very easy way to configure Spring Beans using a DSL Just create grails-app/conf/spring/resources.groovy and add this:

?View Code GROOVY
beans =
{
  memcachedClient(MemcachedClient, 
    net.spy.memcached.AddrUtil.getAddresses(
      ConfigurationHolder.config.memcached_servers))
  { bean ->
    bean.scope = 'singleton' 
  }
}

Voila, from now on you can get a memcached client instance anywhere in your application either by standard dependency injection or by calling applicationContext.getBean(’memcachedClient’).

It is important to note that this example assumes that you have configured your memcached server(s) in grails-app/conf/Config.groovy like this:

?View Code GROOVY
environments
{
  development
  {
    memcached_servers = "192.168.230.2:11211"
  }
 
  production
  {
    memcached_servers = "localhost:11211"
  }
}