Module: RecordCache::ActiveRecord::Base::ClassMethods
- Defined in:
- lib/record_cache/datastore/active_record_30.rb,
lib/record_cache/datastore/active_record_31.rb,
lib/record_cache/datastore/active_record_32.rb
Constant Summary collapse
- RC_TRANSACTIONS_THRESHOLD =
the tests are always run within a transaction, so the threshold is one higher
ENV['RAILS_ENV'] == 'test' ? 1 : 0
Instance Method Summary collapse
-
#find_by_sql_with_record_cache(sql, binds = []) ⇒ Object
Retrieve the records, possibly from cache.
-
#record_cache_init ⇒ Object
add cache invalidation hooks on initialization.
- #try_record_cache(arel, sql, binds) ⇒ Object
Instance Method Details
#find_by_sql_with_record_cache(sql, binds = []) ⇒ Object
Retrieve the records, possibly from cache
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/record_cache/datastore/active_record_30.rb', line 28 def find_by_sql_with_record_cache(sql) # shortcut, no caching please return find_by_sql_without_record_cache(sql) unless record_cache? arel = sql.instance_variable_get(:@arel) sanitized_sql = sanitize_sql(sql) records = if connection.instance_variable_get(:@query_cache_enabled) query_cache = connection.instance_variable_get(:@query_cache) query_cache["rc/#{sanitized_sql}"] ||= try_record_cache(sanitized_sql, arel) elsif connection.open_transactions > RC_TRANSACTIONS_THRESHOLD connection.send(:select, sanitized_sql, "#{name} Load") else try_record_cache(sanitized_sql, arel) end records.collect! { |record| instantiate(record) } if records[0].is_a?(Hash) records end |
#record_cache_init ⇒ Object
add cache invalidation hooks on initialization
21 22 23 24 25 |
# File 'lib/record_cache/datastore/active_record_30.rb', line 21 def record_cache_init after_commit :record_cache_create, :on => :create, :prepend => true after_commit :record_cache_update, :on => :update, :prepend => true after_commit :record_cache_destroy, :on => :destroy, :prepend => true end |
#try_record_cache(arel, sql, binds) ⇒ Object
47 48 49 50 51 52 |
# File 'lib/record_cache/datastore/active_record_30.rb', line 47 def try_record_cache(sql, arel) query = arel && arel.respond_to?(:ast) ? RecordCache::Arel::QueryVisitor.new.accept(arel.ast) : nil record_cache.fetch(query) do connection.send(:select, sql, "#{name} Load") end end |