R – n ORM-like wrapper for memcached


I'm looking for a ruby gem (or rails plugin) which abstracts the details of memcached in the same way that ActiveRecord abstracts the details of SQL. I am NOT looking for something to help cache ActiveRecord models in memcached. I'm sure there are approximately 4215 gems that will help with that problem.

Ideally what I'd like is to be able to do something like:

class Apple < MemcachedModel
# whatever else here

and then be able to do stuff like:

my_apple = Apple.find('some memcached key')

which would look up the JSON representation of this class in memcached and deserialize it. I'd also maybe be able to do things like:

my_apple.color = "red"

# persist changes back to memcached

# load any changes from memcached into local model

It seems like someone must have scratched this itch by now and created something along these lines, but whenever I google for such a gem I just keep turning up thing which help cache AR models using memcached.

Best Solution

You can take a look at my moneta gem, which is an ORM'ish thing for all kinds of key-value-stores. You can see it at: http://github.com/wycats/moneta/tree/master

The basic idea behind moneta is that all KVSs should behave exactly like a subset of normal Ruby hashes. We support:


The store and update_key methods take an additional options hash which you can use thusly:

cache = Moneta::Memcache.new(:server => "localhost:11211", :namespace => "me")
cache.store("name", "wycats", :expires_in => 2)
cache.update_key("name", :expires_in => 10)

We support a large number of KVSs:

  • BerkeleyDB
  • CouchDB
  • DataMapper (which means any store supported by DM)
  • Files
  • LMC
  • Memcache
  • In-process memory
  • MongoDB
  • Redis
  • Tokyo Cabinet
  • Tokyo Tyrant
  • S3
  • SDBM
  • Files using XAttrs

Every store supports expiry, either natively (like in memcached) or using a standard module that emulates memcache-style expiry. The API is always identical and there is a shared spec that all adapters are run against to ensure compliance.

It is also quite easy to add your own adapter, which is why so many exist.

Related Question