class RecordCache::Strategy::Base
Public Class Methods
# File lib/record_cache/strategy/base.rb, line 10 def initialize(base, attribute, record_store, options) @base = base @attribute = attribute @record_store = with_multi_support(record_store) @cache_key_prefix = "rc/#{options[:key] || @base.name}/" @version_opts = options[:ttl] ? { :ttl => options[:ttl] } : {} end
Parse the options and return (an array of) instances of this strategy.
# File lib/record_cache/strategy/base.rb, line 6 def self.parse(base, record_store, options) raise NotImplementedError end
Public Instance Methods
Retrieve the attribute
for this strategy (unique per model). May be a non-existing attribute in case a cache is not based on a single attribute.
# File lib/record_cache/strategy/base.rb, line 20 def attribute @attribute end
Can the cache retrieve the records based on this query?
# File lib/record_cache/strategy/base.rb, line 34 def cacheable?(query) raise NotImplementedError end
Fetch all records and sort and filter locally
# File lib/record_cache/strategy/base.rb, line 25 def fetch(query) records = fetch_records(query) Util.filter!(records, query.wheres) if query.wheres.size > 0 Util.sort!(records, query.sort_orders) if query.sorted? records = records[0..query.limit-1] if query.limit records end
Handle invalidation call
# File lib/record_cache/strategy/base.rb, line 44 def invalidate(id) version_store.renew(cache_key(id), version_opts) end
Handle create/update/destroy (use record.previous_changes to find the old values in case of an update)
# File lib/record_cache/strategy/base.rb, line 39 def record_change(record, action) raise NotImplementedError end
Protected Instance Methods
retrieve the cache key for the given id, e.g. rc/person/14
# File lib/record_cache/strategy/base.rb, line 77 def cache_key(id) "#{@cache_key_prefix}#{id}" end
# File lib/record_cache/strategy/base.rb, line 50 def fetch_records(query) raise NotImplementedError end
retrieve the record store (store for records for this cache strategy)
# File lib/record_cache/strategy/base.rb, line 67 def record_store @record_store end
find the statistics for this cache strategy
# File lib/record_cache/strategy/base.rb, line 72 def statistics @statistics ||= RecordCache::Statistics.find(@base, @attribute) end
should be used when calling version_store.renew(…, version_opts
)
# File lib/record_cache/strategy/base.rb, line 62 def version_opts @version_opts end
retrieve the version store (unique store for the whole application)
# File lib/record_cache/strategy/base.rb, line 57 def version_store RecordCache::Base.version_store end
retrieve the versioned record key, e.g. rc/person/14v1
# File lib/record_cache/strategy/base.rb, line 82 def versioned_key(cache_key, version) "#{cache_key}v#{version.to_s}" end
Private Instance Methods
add default implementation for multi support to perform multiple cache calls in a pipelined fashion
# File lib/record_cache/strategy/base.rb, line 89 def with_multi_support(cache_store) unless cache_store.respond_to?(:multi) cache_store.send(:define_singleton_method, :multi) do |&block| block.call end end cache_store end