# FindCache

It is simply an ActiveRecord object caching gem using Rails.cache methods.

It makes ActiveRecord ‘find_by_id, find_by_(attr)’ methods and ‘has_one, belongs_to relations’ cacheable by id or any referenced columns using Rails.cache methods. It also supports fetching multiple records using ids with find_all_cache method(if cache store supports multiple reads [hint: memcached_store, dalli_store supports.]).

## Installation

Add this line to your application’s Gemfile:

gem 'find_cache'

And then execute:

$ bundle

Or install it yourself as:

$ gem install find_cache

## For has_one and belongs_to relations

### Sample Models:

User (‘has_one :user_detail`) ->

find_cache_has_one :user_detail, UserDetail, :user_id

find_cache_has_one attr_name_or_any_other_name, ModelName, foreign_key
  • name

  • email

  • username

UserDetail (‘belongs_to :user`) ->

find_cache_belongs_to :user, User, :user_id

find_cache_belongs_to attr_name_or_any_other_name, ModelName, foreign_key
  • user_id

  • avatar

  • age

  • sex

  • location

## For finding a record

user = User.find_cache(id) # fetches from cache
user.user_detail # fetches from cache if 'find_cache_has_one :user_detail, UserDetail, :user_id' added to User model 

user = User.find_by_id(id) # fetches from DB
user.user_detail # fetches from DB

## For fetching multiple ids

users = User.find_all_cache([1,2,3,4,5])

## For fetching a record by attribute

user_detail = UserDetail.find_by_user_id(1) # from db
user_detail = UserDetail.find_cache_by_ref(:user_id, 1) # from cache store

## For counter_cache ‘cache invalidation’

### Create a ruby file under config/initializers and add the following codes:

module ActiveRecord
  module CounterCache
    def increment_counter(counter_name, id)
      expire_find_cache(name, id)
      update_counters(id, counter_name => 1)
    end

    def decrement_counter(counter_name, id)
      expire_find_cache(name, id)
      update_counters(id, counter_name => -1)
    end

    def expire_find_cache(name, id)
      key = FindCache::KeyGen.cache_key(name, id)
      Rails.cache.delete(key)
      $find_cache_store[FindCache::KeyGen.global_cache_key].delete(key)
    end
  end
end

## Notes

Tested with dalli (github.com/mperham/dalli).

## Credits

FindCache is part of videofork.com project.

## 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