Module: SecondLevelCache::ActiveRecord::FinderMethods
- Defined in:
- lib/second_level_cache/active_record/finder_methods.rb
Instance Method Summary collapse
Instance Method Details
#find_one(id) ⇒ Object
TODO: find_some api.rubyonrails.org/classes/ActiveRecord/FinderMethods.html#method-i-find_one
Cacheable:
current_user.articles.where(status: 1).visiable.find(params[:id])
Uncacheable:
Article.where("user_id = '1'").find(params[:id])
Article.where("user_id > 1").find(params[:id])
Article.where("articles.user_id = 1").find(prams[:id])
Article.where("user_id = 1 AND ...").find(params[:id])
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/second_level_cache/active_record/finder_methods.rb', line 19 def find_one(id) return super(id) unless second_level_cache_enabled? return super(id) unless select_all_column? id = id.id if ActiveRecord::Base == id if cachable? record = @klass.read_second_level_cache(id) if record if where_values_hash.blank? || where_values_match_cache?(record) return record end end end record = super(id) record.write_second_level_cache record end |
#first(limit = nil) ⇒ Object
api.rubyonrails.org/classes/ActiveRecord/FinderMethods.html#method-i-first
Cacheable:
User.where(id: 1).first
User.where(id: 1).last
Uncacheable:
User.where(name: 'foo').first
User.where(age: 18).first
50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/second_level_cache/active_record/finder_methods.rb', line 50 def first(limit = nil) return super(limit) if limit.to_i > 1 # only have primary_key condition in where if where_values_hash.length == 1 && where_values_hash.key?(primary_key) record = @klass.read_second_level_cache(where_values_hash[primary_key]) return record if record end record = super(limit) record&.write_second_level_cache record end |