Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redis backed Rails model

I am looking for something where I can keep an entire Rails based Model stored in Redis. There is Redis Objects that is found here https://github.com/nateware/redis-objects but that only works when your model is already backed by something like ActiveRecord and it requires a unique id generator. I don't wish to make an ActiveRecord backed model, as I want to persist everything directly into memory and not into the database.

Is there a drop in tool that I can use right now that'll let me do things like:

RedisBackedModel.find_by_name('foo')

and it'll retrieve me the RedisBackedModel from Redis?

like image 773
randombits Avatar asked Apr 04 '12 01:04

randombits


1 Answers

I've used Ohm before for storing searches that expire after a couple hours; it's pretty nice. I think DataMapper also has a redis adapter, although I've never used it. Here's some Ohm code for what I think you're after:

class RedisBackedModel < Ohm::Model
  attribute :name
  index :name
end

rbm = RedisBackedModel.create :name => "foo"
rbm.id # => 1

# Search by name:
RedisBackedModel.find(:name => "foo")

# Search by id (like AR-style model.get(id)):
RedisBackedModel[1]
like image 104
Abe Voelker Avatar answered Oct 04 '22 21:10

Abe Voelker