Module: CaChing::ReadThrough::InstanceMethods

Defined in:
lib/ca_ching/read_through.rb

Instance Method Summary collapse

Instance Method Details

#cacheable?Boolean

Returns:

  • (Boolean)


40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ca_ching/read_through.rb', line 40

def cacheable?
  unsupported_methods = [:from_value,
                         :group_values,
                         :having_values,
                         :includes_values,
                         :joined_includes_values,
                         :joins_values,
                         :lock_value,
                         :select_values]
  !where_values.empty? && find_on_indexed_fields? && unsupported_methods.inject(true) { |flag, method| self.send(method).send(method.to_s =~ /values/ ? :empty? : :nil?) && flag }
end

#from_cache?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/ca_ching/read_through.rb', line 36

def from_cache?
  @from_cache ||= false
end

#to_a_with_cacheObject

All queries, be they find(id), dynamic finders (find_by_foo, find_all_by_foo, etc.), where(:foo => :bar), order(‘foo DESC’), etc. go through to_a before being returned. Hook into that point to check the cache.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ca_ching/read_through.rb', line 16

def to_a_with_cache
  @_query = CaChing::Query::Select.new(self)
  
  return to_a_without_cache if CaChing.cache.nil? || CaChing.disabled? || !cacheable?
  
  result = CaChing.cache.find(@_query)
  @from_cache = true
  
  if result.nil?
    result = to_a_without_cache
    CaChing.cache.insert(result, :for => @_query)
    @from_cache = false
  end
  
  result.from_cache = self.from_cache?
  result.each { |item| item.from_cache = self.from_cache? }
  
  return result
end