Skip to content

terrcin/spandex_mem_cache_store

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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 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 = 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:

Grab the plugin:

ruby script/plugin install git://github.com/terrcin/spandex_mem_cache_store.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, '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 doing the things 
described above.


Location:

https://github.com/terrcin/spandex_mem_cache_store


Requirements:

Rails >= 2.1


Recommended Plugins:

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

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:

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

About

Enhanced memcache store support for super heroes

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages