Project

find_cache

0.0
No commit activity in last 3 years
No release in over 3 years
Makes ActiveRecord 'find_by_id, find_by_(attr)' methods and 'has_one, belongs_to' relations are cacheable by PrimaryKey(PK) or any referenced columns using Rails.cache methods. It also supports fetching multiple records using PKs(ids) with find_all_cache method(if cache store supports multiple reads [hint: memcached_store, dalli_store supports.]).
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.10
~> 4.5.0
~> 10.0
~> 3.4.0
~> 1.3.11

Runtime

>= 3.0.0
>= 3.0.0
 Project Readme

FindCache¶ ↑

<img src=“https://codeclimate.com/github/mustafaturan/find_cache/badges/gpa.svg” /> <img src=“https://codeclimate.com/github/mustafaturan/find_cache/badges/issue_count.svg” /> <img src=“https://travis-ci.org/mustafaturan/find_cache.svg?branch=master” alt=“Build Status” />

It is simply a thread-safe 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 PrimaryKey(PK) or any referenced columns using Rails.cache methods. It also supports fetching multiple records using PKs(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:

class User < ActiveRecord::Base
  has_one :user_detail

  # to enable has_one caching
  find_cache_has_one :user_detail, UserDetail, :user_id 
end

class UserDetail < ActiveRecord::Base
  belongs_to :user

  # to enable belongs_to caching
  find_cache_belongs_to :user, User, :user_id 
end

For finding a record¶ ↑

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

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])
users_ordered_desc = User.find_all_cache([4,5,1,2,3], 'id DESC')
users_ordered_asc = User.find_all_cache([4,5,1,2,3], 'id ASC')

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

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