Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an ORM-like wrapper for memcached

Tags:

ruby

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
end

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
my_apple.save 

# load any changes from memcached into local model
my_apple.update

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.

like image 788
Pete Hodgson Avatar asked Jun 30 '09 16:06

Pete Hodgson


People also ask

Does Memcached support replication?

Memcache does not support replication. Nor does it store any data on disk. Everything is stored on memory. This is the main reason memcache is so fast.

Should I use Memcache or Redis?

Redis uses a single core and shows better performance than Memcached in storing small datasets when measured in terms of cores. Memcached implements a multi-threaded architecture by utilizing multiple cores. Therefore, for storing larger datasets, Memcached can perform better than Redis.

Why Redis is better than memcache?

Although they are both easy to use and offer high performance, there are important differences to consider when choosing an engine. Memcached is designed for simplicity while Redis offers a rich set of features that make it effective for a wide range of use cases.

Is Memcached still used?

The primary cache is still Memcached, and Redis is used for its data structures based logical caching.


1 Answers

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:

#[]
#[]=
#delete
#fetch
#key?
#store
#update_key
#clear

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.

like image 99
Yehuda Katz Avatar answered Sep 23 '22 12:09

Yehuda Katz