ShadowModel
A rails plugin use redis to cache models data.
Installation
Add this line to your application's Gemfile:
gem 'shadow_model'
And then execute:
$ bundle
Usage
Add this to your model class, then the models will be cached with the assigned attributes and methods after the model saved or updated.
shadow_model attribute_or_method1, attribute_or_method2, ...,
And use this to retrieve the model from redis.
YourModelClass.find_by_shadow(primary_key)
options
| expiration | Set the timeout of each cache. |
| update_expiration | Reset cache expiration after model updated. | expireat | Set the absolute timeout timestamp of each cache. |
Example
# == Schema Information
#
# Table name: players
#
# id :integer not null, primary key
# name :string(255)
# stamina :integer
# created_at :datetime not null
# updated_at :datetime not null
class Player < ActiveRecord::Base
shadow_model :name, :stamina, :cacheable_method, expiration: 30.minutes
def cacheable_method
"result to cache"
end
end
