class RecordCache::Strategy::FullTableCache

Constants

FULL_TABLE

Public Class Methods

parse(base, record_store, options) click to toggle source

parse the options and return (an array of) instances of this strategy

# File lib/record_cache/strategy/full_table_cache.rb, line 7
def self.parse(base, record_store, options)
  return nil unless options[:full_table]
  return nil unless base.table_exists?

  FullTableCache.new(base, :full_table, record_store, options)
end

Public Instance Methods

cacheable?(query) click to toggle source

Can the cache retrieve the records based on this query?

# File lib/record_cache/strategy/full_table_cache.rb, line 15
def cacheable?(query)
  true
end
record_change(record, action) click to toggle source

Clear the cache on any record change

# File lib/record_cache/strategy/full_table_cache.rb, line 20
def record_change(record, action)
  version_store.delete(cache_key(FULL_TABLE))
end

Protected Instance Methods

cache_key(id) click to toggle source
Calls superclass method RecordCache::Strategy::Base#cache_key
# File lib/record_cache/strategy/full_table_cache.rb, line 47
def cache_key(id)
  super(FULL_TABLE)
end
fetch_records(query) click to toggle source

retrieve the record(s) with the given id(s) as an array

# File lib/record_cache/strategy/full_table_cache.rb, line 27
def fetch_records(query)
  key = cache_key(FULL_TABLE)
  # retrieve the current version of the records
  current_version = version_store.current(key)
  # get the records from the cache if there is a current version
  records = current_version ? from_cache(key, current_version) : nil
  # logging (only in debug mode!) and statistics
  log_full_table_cache_hit(key, records) if RecordCache::Base.logger.debug?
  statistics.add(1, records ? 1 : 0) if statistics.active?
  # no records found?
  unless records
    # renew the version in case the version was not known
    current_version ||= version_store.renew_for_read(key, version_opts)
    # retrieve all records from the DB
    records = from_db(key, current_version)
  end
  # return the array
  records
end

Private Instance Methods

from_cache(key, version) click to toggle source

retrieve the records from the cache with the given keys

# File lib/record_cache/strategy/full_table_cache.rb, line 56
def from_cache(key, version)
  records = record_store.read(versioned_key(key, version))
  records.map{ |record| Util.deserialize(record) } if records
end
from_db(key, version) click to toggle source

retrieve the records with the given ids from the database

# File lib/record_cache/strategy/full_table_cache.rb, line 62
def from_db(key, version)
  RecordCache::Base.without_record_cache do
    # retrieve the records from the database
    records = @base.all.to_a
    # write all records to the cache
    record_store.write(versioned_key(key, version), records.map{ |record| Util.serialize(record) })
    records
  end
end
log_full_table_cache_hit(key, records) click to toggle source

log cache hit/miss to debug log

# File lib/record_cache/strategy/full_table_cache.rb, line 75
def log_full_table_cache_hit(key, records)
  RecordCache::Base.logger.debug{ "FullTableCache #{records ? 'hit' : 'miss'} for model #{@base.name}" }
end