Nick Kallen’s cache-money gem provides super easy caching for your Rails apps. Install memcached and the gem and then drop in the provided config file and you’re off and running.
The only snag you’ll probably run into is that cache-money is loaded in development mode. This sucks as you’ll run into odd errors as you develop your application.
Whilst talking to Obie Fernandez, he dropped a nugget of wisdom that lead to this method:
if RAILS_ENV != 'development'
require 'cache_money'
config = YAML.load(IO.read(File.join(RAILS_ROOT, "config", "memcached.yml")))[RAILS_ENV]
$memcache = MemCache.new(config)
$memcache.servers = config['servers']
$local = Cash::Local.new($memcache)
$lock = Cash::Lock.new($memcache)
$cache = Cash::Transactional.new($local, $lock)
class ActiveRecord::Base
is_cached :repository => $cache
end
else
# If we're in development mode, we don't want to
# deal with caching oddities, so let's override
# cache-money's #index method to do nothing…
class ActiveRecord::Base
def self.index(*args)
end
end
end
Basically, this wraps the cache-money’s config stuff in a if/else statment and then override’s cache-money’s #index method so that errors won’t be thrown when Rails encounters them in your indexed models.






