evansweb.info

That's Numberwang!

Ruby memcache-client enhancement

Posted by Jon Evans 23/08/2006 at 08h26

Recently I have been refactoring some code so that various computed results are saved in a memcached cache to improve performance.

A common cache idiom is:

  1. Compute cache key
  2. look up key in cache
  3. if value found, return it
  4. else, run some code to generate the required value
  5. save the generated value in the cache
  6. return the value

It's quite awkward to have that code repeated all over your app, so I hit upon the idea of passing a block to Cache#get() which would be used to compute the value if the key wasn't found in the cache.

2 comments |