Skip to content
Enhanced memcache store support for super heroes
Ruby
Find file
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
lib
MIT-LICENSE
README
change.log
init.rb

README

Spandex MemCache Store
======================

Description:

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.


Features:

* Local cache used to buffer duplicate gets per request

Standard caching techniques involve reading from the cache within the action to see if the view
is cached, if not then execute expensive code as the view will need to be rendered.  

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 does what it's needed todo for the view, so lots of weird errors 
about instance vars not being nil etc... appear.  This is a very frustrating problem to have 
once you have figured out whats 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.

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 = MemCache.new('192.168.0.34', {: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.


Installation:

Install the plugin under vendor/plugins and 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, '127.0.0.1', {:namespace => "mywebsite-#{RAILS_ENV}"}


Usage:

Nothing new or special yet.  Just drop it in as above and it'll instantly start do the things 
described above.


Requirements:

Rails >= 2.1


Recommended Plugins:

* XML Cache
http://code.google.com/p/xmlcache/


Future Plans:

* Make this a Gem
* Conditional caching
* Multi-Get
* Dynamic key helpers
* Make the code more awesome

Latest details and dicussion can be found at a dedicated page on my blog:

www.motionstandingstill.com/spandex-mem-cache-store


Credit:

The local caching concept I first encountered when using the ExtendedFragmentCache plugin 
(http://rubyforge.org/projects/zventstools/) back in 2006. I then re-factored that for 
Ponoko.com when I was working there.  This plugin is a completely new implementation.


Contact Details:

Nahum Wild
email: nahum.wild@gmail.com
blog: www.motionstandingstill.com
Something went wrong with that request. Please try again.