Module: RecordCache::ActiveRecord::Base::ClassMethods

Defined in:
lib/record_cache/datastore/active_record_30.rb,
lib/record_cache/datastore/active_record_31.rb

Instance Method Summary collapse

Instance Method Details

#find_by_sql_with_record_cache(*args) ⇒ Object

Retrieve the records, possibly from cache



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/record_cache/datastore/active_record_30.rb', line 27

def find_by_sql_with_record_cache(*args)
   # no caching please
  return find_by_sql_without_record_cache(*args) unless record_cache?

  # check the piggy-back'd ActiveRelation record to see if the query can be retrieved from cache
  sql = args[0]
  arel = sql.instance_variable_get(:@arel)
  query = arel ? RecordCache::Arel::QueryVisitor.new.accept(arel.ast) : nil
  cacheable = query && record_cache.cacheable?(query)
  # log only in debug mode!
  RecordCache::Base.logger.debug("#{cacheable ? 'Fetch from cache' : 'Not cacheable'} (#{query}): SQL = #{sql}") if RecordCache::Base.logger.debug?
  # retrieve the records from cache if the query is cacheable otherwise go straight to the DB
  cacheable ? record_cache.fetch(query) : find_by_sql_without_record_cache(*args)
end

#record_cache_initObject

add cache invalidation hooks on initialization



20
21
22
23
24
# File 'lib/record_cache/datastore/active_record_30.rb', line 20

def record_cache_init
  after_commit :record_cache_create,  :on => :create
  after_commit :record_cache_update,  :on => :update
  after_commit :record_cache_destroy, :on => :destroy
end