Redis backed Rails model

Go To StackoverFlow.com

5

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?

2012-04-04 01:57
by randombits


6

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]
2012-04-04 03:05
by Abe Voelker
Did you used OHM with rails - Pravin Mishra 2014-01-07 12:22
They are complaining "Unfortunately we don't use ohm with rails at all (and never plan to)." https://github.com/soveran/ohm/issues/2 - Pravin Mishra 2014-01-07 12:23
@PravinMishra Yes, I used it on a project a couple years ago when Rails 3.0 had just come out. It works fine - Abe Voelker 2014-01-12 00:14
thanks for the reference to Ohm! https://www.youtube.com/watch?v=QLwEG3cdeRw : - Tilo 2016-09-23 02:26