Redis::Objects::RMap

Adds Redis-cached hash containing correspondence between row id and selected field to ActiveRecord models.

Travis CI

Installation

Add this line to your application's Gemfile:

gem 'redis-objects-rmap'

And then execute:

$ bundle

Or install it yourself as:

$ gem install redis-objects-rmap

Usage

class Foo < ActiveRecord::Base
  include Redis::Objects::RMap
  has_rmap :title # field to use as a title
end

Foo.create! :title => 'foo'
Foo.rmap # {1 => 'foo'} <- Does SQL request and puts to Redis
Foo.rmap # {1 => 'foo'} <- Gets from Redis without an SQL query

You can specify field to use as an ID source:

class Foo < ActiveRecord::Base
  include Redis::Objects::RMap
  has_rmap :title, :my_id
end

Or even specify lambdas to prepare your cache:

class Foo < ActiveRecord::Base
  include Redis::Objects::RMap
  has_rmap :title => lambda{|x| x.camelize}, :id => lambda{|x| x.to_s}
end
class Foo < ActiveRecord::Base
  include Redis::Objects::RMap
  has_rmap({:title => lambda{|x| x.camelize}}, :my_id)
end

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request