Find file
Fetching contributors…
Cannot retrieve contributors at this time
113 lines (67 sloc) 3.78 KB
Spandex MemCache Store
A enhanced version of and replacement for the MemCacheStore shipping with rails. It uses a
per-request local cache to buffer duplicate memcache reads which can result in halving read
requests, and it uses a single connection to memcache for both the cache and session stores.
* Local cache used to buffer duplicate gets per request
Standard fragment caching techniques involve reading from the cache within the action to see
if the view is cached and then again reading the same key from the cache in the view.
There is a rare situation on websites with good levels of traffic where the cache is populated
when the action checks, but before the view is executed a separate request expires/deletes
that cache entry resulting in the view being rendered rather than pulled from the cache. In
this case the action hasn't done what it's needed to do for the view, so lots of weird errors
about instance vars being nil etc... appear. This is a very frustrating problem to have
once you have figured out what is actually causing it.
SpandexMemCacheStore caches the result from the action's cache read and returns that to the
view without going to memcache a second time. This also occurs for session reads, Rails does
a minimum of two reads per request, this local caching cuts that down to one.
For a dynamic web 2.0 site which has several layers of fragment caching this can halve the
number of reads from memcache per request with a populated cache. Thus speeding up the site.
The SpandexMemCacheStore's local cache is cleared before each request is executed.
* Easy configuration and single connection to memcache per app
IMO the rails session MemCacheStore is broken, if you simply specify :mem_cache_store it will
only talk to a memcache server running on localhost at the default port of 11211. To get it to
work properly you have to duck punch the ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS
constant like this:
CACHE ='', {:namespace => "mywebsite-#{RAILS_ENV}"})
ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS.merge!({'cache' => CACHE})
To stop this from happening SpandexMemcacheStore re-uses the connection to memcache established
through with the caching session store.
Grab the plugin:
ruby script/plugin install git://
Then in environment.rb within the Initializer area place the following:
config.action_controller.session_store = :spandex_mem_cache_store
config.cache_store = :spandex_mem_cache_store, '', {:namespace => "mywebsite-#{RAILS_ENV}"}
Nothing new or special yet. Just drop it in as above and it'll instantly start doing the things
described above.
Rails >= 2.1
Recommended Plugins:
* XML Cache
Fragment caching for xml :-)
Future Plans:
Basically this is the start of me releasing a bunch of work that I've done in this area, I intend
to tidy the code up a bit more as time goes buy and include the following:
* TESTS! (I know, not had time, but it is used on at least one live site)
* Make this a Gem
* Conditional caching
* Multi-Get
* Dynamic key helpers
* Make the code more awesome
Latest details and discussion can be found at a dedicated page on my blog:
The local caching concept I first encountered when using the ExtendedFragmentCache plugin
( back in 2006. I then re-factored that for when I was working there. This plugin is a completely new implementation.
Contact Details:
Nahum Wild