CacheLookup
Provide an alternative to ActiveRecord#find that uses the Rails cache to store retrieved items to reduce database load and increase performance for subsequent lookups for the same item. CacheLookup also provides cache expiry for items when they are updated or deleted from the database.
Installation
Add this line to your application's Gemfile:
gem 'cache_lookup'
And then execute:
$ bundle
Or install it yourself as:
$ gem install cache_lookup
Quick Start
In your ActiveRecord model:
class Article < ActiveRecord::Base
cache_lookup :id
cache_lookup :slug
end
In your controller:
class ArticleController < ApplicationController
def show
@article = Article.lookup_by_id(params[:id])
end
end
Usage
cache_lookup(attribute)
Defines the attribute to use as the cache key, and for reading from the database. The attribute must return a single item when queried, otherwise cache expiry will be broken.
Typically, the attribute will be 'id', but can be any attribute on the model.
class Article < ActiveRecord::Base
cache_lookup :id
end
Model#lookup_by_
The method lookup_by_ will be added to your model. In the above example, this will be lookup_by_id. When used with the 'id' attribute, this can be a direct replacement for Model.find(id).
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Added some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
Credits
CacheLookup is maintained by CRAZ8