module RecordCache::ActiveRecord::Base::ClassMethods

Constants

RC_TRANSACTIONS_THRESHOLD

the tests are always run within a transaction, so the threshold is one higher

Public Instance Methods

find_by_sql_with_record_cache(sql) click to toggle source

Retrieve the records, possibly from cache

# 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.query_cache_enabled
              connection.query_cache["rc/#{sanitized_sql}"] ||= try_record_cache(sanitized_sql, arel)
            elsif connection.open_transactions > RC_TRANSACTIONS_THRESHOLD
              connection.select_all(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() click to toggle source

add cache invalidation hooks on initialization

# 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(sql, arel) click to toggle source
# File lib/record_cache/datastore/active_record_30.rb, line 46
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.select_all(sql, "#{name} Load")
  end
end