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_by_attributes_with_second_level_cache(match, attributes, *args) ⇒ Object

TODO cache find_or_create_by_id



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/second_level_cache/active_record/finder_methods.rb', line 52

def find_by_attributes_with_second_level_cache(match, attributes, *args)
  return find_by_attributes_without_second_level_cache(match, attributes, *args) unless second_level_cache_enabled?

  conditions = Hash[attributes.map {|a| [a, args[attributes.index(a)]]}]

  if conditions.has_key?("id")
    result = wrap_bang(match.bang?) do
      if conditions.size == 1
        find_one_with_second_level_cache(conditions["id"])
      else
        where(conditions.except("id")).find_one_with_second_level_cache(conditions["id"])
      end
    end
    yield(result) if block_given? #edge rails do this bug rails3.1.0 not

    return result
  end

  find_by_attributes_without_second_level_cache(match, attributes, *args)
end

#find_one_with_second_level_cache(id) ⇒ Object

TODO fetch multi ids

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


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/second_level_cache/active_record/finder_methods.rb', line 28

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

  id = id.id if ActiveRecord::Base === id
  if ::ActiveRecord::IdentityMap.enabled? && cachable? && record = from_identity_map(id)
    return record
  end

  if cachable?
    return record if record = @klass.read_second_level_cache(id)
  end

  if cachable_without_conditions?
    if record = @klass.read_second_level_cache(id)
      return record if where_match_with_cache?(where_values, record)
    end
  end

  record = find_one_without_second_level_cache(id)
  record.write_second_level_cache
  record
end