Takes care of exceptions like Memcached::NotFound and also forking/threading.
It uses a (useless?) memory store by default:
cache = Cache.new cache.set 'hello' cache.get 'hello', 'world'
You can specify a more useful cache client:
require 'memcached' # a really fast memcached client gem require 'cache' # this gem, which provides a standard interface raw_client = Memcached.new '127.0.0.1:11211' cache = Cache.new raw_client
require 'redis' # the redis key-value store require 'cache' # this gem, which provides a standard interface raw_client = Redis.new cache = Cache.new raw_client
require 'dalli' # the dalli memcached client used by heroku require 'cache' # this gem, which provides a standard interface raw_client = Dalli::Client.new cache = Cache.new raw_client
Maybe this will even work:
# Piggyback off the default rails cache cache = Cache.new Rails.cache
cache.get 'hello' cache.set 'hello', 'world' cache.delete 'hello' cache.flush
Supported memcached clients:
memcached (either a Memcached or a Memcached::Rails)
dalli (either a Dalli::Client or an ActiveSupport::Cache::DalliStore)
memcache-client (MemCache, the one commonly used by Rails)
Supported Redis clients:
0 means don't expire.
When you use a Cache object to wrap Memcached or Redis, you don't have to worry about forking or threading.
For example, you don't have to set up unicorn or PhusionPassenger's after_fork.
I wanted a common interface to a bunch of great Ruby cache clients so I can develop gems (lock_method, cache_method) that accept any of them.
I am so tired of rescuing from Memcached::NotFound
I am so tired of forgetting whether it's :expires_in or :ttl
I don't know why we ever started using read/write instead of get/set.
I don't like how you have to manually handle after_fork for Redis, Memcached, etc.
Copyright 2011 Seamus Abshere