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:
- Compute cache key
- look up key in cache
- if value found, return it
- else, run some code to generate the required value
- save the generated value in the cache
- 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.