Module: SecondLevelCache::ActiveRecord::FinderMethods

Extended by:
ActiveSupport::Concern
Defined in:
lib/second_level_cache/active_record/finder_methods.rb

Instance Method Summary collapse

Instance Method Details

#find_one_with_second_level_cache(id) ⇒ Object

TODO find_some github.com/rails/rails/blob/master/activerecord/lib/active_record/relation/finder_methods.rb#L289-L309

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])


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/second_level_cache/active_record/finder_methods.rb', line 24

def find_one_with_second_level_cache(id)
  return find_one_without_second_level_cache(id) unless second_level_cache_enabled?
  return find_one_without_second_level_cache(id) unless select_all_column?

  id = id.id if ActiveRecord::Base === id

  if cachable?
    record = @klass.read_second_level_cache(id)
    if record
      return record if where_values.blank? || where_values_match_cache?(record)
    end
  end

  record = find_one_without_second_level_cache(id)
  record.write_second_level_cache
  record
end