Module: CaChing::ReadThrough
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/ca_ching/read_through.rb
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
- #cacheable? ⇒ Boolean
- #from_cache? ⇒ Boolean
-
#to_a_with_cache ⇒ Object
All queries, be they find(id), dynamic finders (find_by_foo, find_all_by_foo, etc.), where(:foo => :bar), order(‘foo DESC’), etc.
Instance Method Details
#cacheable? ⇒ Boolean
39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/ca_ching/read_through.rb', line 39 def cacheable? unsupported_methods = [:from_value, :group_values, :having_values, :includes_values, :joined_includes_values, :joins_values, :lock_value, :select_values, :order_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
35 36 37 |
# File 'lib/ca_ching/read_through.rb', line 35 def from_cache? @from_cache ||= false end |
#to_a_with_cache ⇒ Object
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.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/ca_ching/read_through.rb', line 15 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 |