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

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


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

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

#find_some_with_second_level_cache(ids) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/second_level_cache/active_record/finder_methods.rb', line 41

def find_some_with_second_level_cache(ids)
  return find_some_without_second_level_cache(ids) unless second_level_cache_enabled?
  return find_some_without_second_level_cache(ids) unless select_all_column?

  if cachable?
    result = multi_read_from_cache(ids)
  else
    result = where(:id => ids).all
  end

  expected_size =
    if limit_value && ids.size > limit_value
      limit_value
    else
      ids.size
    end

  # 11 ids with limit 3, offset 9 should give 2 results.
  if offset_value && (ids.size - offset_value < expected_size)
    expected_size = ids.size - offset_value
  end

  if result.size == expected_size
    result
  else
    raise_record_not_found_exception!(ids, result.size, expected_size)
  end
end