Is there an ORM-like wrapper for memcached

I am looking for a ruby ​​pearl (or rails plugin) that abstracts memcached details in the same way that ActiveRecord abstracts SQL details. I am NOT looking for something to help cache ActiveRecord models in memcached. I am sure with about 4215 gems that will help with this problem.

Ideally, I would like to do something like:

class Apple < MemcachedModel # whatever else here end 

and then you can do things like:

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

which will look for the JSON representation of this class in memcached and deserialize it. Perhaps I can also do something 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 in these lines, but whenever I use Google for such a gem, I just keep rolling things that help cache AR models using memcached.

+6
ruby memcached
source share
5 answers

You can take a look at my moneta gem, which is ORM'ish for all kinds of stores with key values. You can see this: http://github.com/wycats/moneta/tree/master

The main idea of ​​the coin is that all KVS should behave exactly like a subset of regular ruby ​​hashes. We support:

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

The store and update_key use an additional hash, which you can use this way:

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

  • Berkeleydb
  • Couchdb
  • DataMapper (which means any storage supported by DM)
  • Files
  • LMC
  • Memcache
  • Memory in process
  • Mongodb
  • Redis
  • Tokyo office
  • Tokyo tyrant
  • S3
  • SDBM
  • Files Using XAttrs

Each store supports expiration, either initially (for example, in memcached), or using a standard module that emulates the expiration of memcache. The API is always identical, and there is a common specification for which all adapters are implemented to ensure compliance.

It is also quite easy to add your own adapter, so many of them exist.

+11
source share

I do not know any Ruby ActiveRecord adapter for Memcached. It would probably be difficult to create a similar library because Memcached does not act like a relational database.

As a result, the library will not be able to implement about 80% of the functions supported by ActiveRecord, so what is the use of such an implementation? You already have everything you need in Rails to work with memcache with a CRUD template.

 Rails.cache.read('key') Rails.cache.write('key', 'value') Rails.cache.delete('key') Rails.cache.increment('key', 5) Rails.cache.fetch('key') { 'value' } 

If you feel more comfortable, you can create a wrapper and proxy these methods using the appropriate new / create / update / save / destroy methods. However, you can never go beyond the basic CRUD system just because Memcached is not intended for a relational database.

+3
source share

This is pretty easy to implement.

 require 'ostruct' require 'active_support/cache' class StoredStruct < OpenStruct attr_writer :store def self.store @store || superclass.store end def self.expand_key(key) 'StoredStruct_' + (superclass == OpenStruct ? '' : "#{self}_") + key.to_s end def self.get_unique_id key = expand_key('unique_id') store.write(key, 0, :unless_exist => true) store.increment(key) end def self.save(instance) id = instance.id || get_unique_id store.write(expand_key(id), instance) id end def self.find(id) store.read(expand_key(id)) end attr_reader :id def attributes @table end def attributes=(hash) @table = hash end def new_record? self.id.nil? end def save @id = self.class.save(self) true end def reload instance = self.class.find(self.id) self.attributes = instance.attributes unless self == instance self end end 

Use it as follows:

 # connect to memcached StoredStruct.store = ActiveSupport::Cache::MemCacheStore.new("localhost:11211") class Apple < StoredStruct end fruit = Apple.new fruit.color = "red" fruit.taste = "delicious" fruit.id #=> nil fruit.save #=> true fruit.id #=> 1 # to load any changes: fruit.reload Apple.find(1) #=> fruit 
+2
source share

As Simone Carletti wrote, Memcached is not a relational database; he cannot even list all his keys. Thus, any ActiveRecord-like model that stores data in Memcached will not contain all ActiveRecord functions. However, I think there is some value in having a compatible API for all your models, so if it makes sense for one of your models to store their data in Memcached, you can use this module, which I created for this purpose:

http://blog.slashpoundbang.com/post/1455548868/memcachemodel-make-any-ruby-object-that-persists-in

+1
source share

You may be looking for Nick Cullen’s cash .

0
source share

All Articles