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,
lib/record_cache/datastore/active_record_40.rb,
lib/record_cache/datastore/active_record_41.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

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
# 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_initObject

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



46
47
48
49
50
51
# 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