0.01
No commit activity in last 3 years
No release in over 3 years
Provide a cache system for single ActiveRecord objects with expiry on update and delete
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

>= 3.0
 Project Readme

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

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

Credits

CacheLookup is maintained by CRAZ8