class RecordCache::Strategy::Base

Public Class Methods

new(base, attribute, record_store, options) click to toggle source
# 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(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/base.rb, line 6
def self.parse(base, record_store, options)
  raise NotImplementedError
end

Public Instance Methods

attribute() click to toggle source

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
cacheable?(query) click to toggle source

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(query) click to toggle source

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
invalidate(id) click to toggle source

Handle invalidation call

# File lib/record_cache/strategy/base.rb, line 44
def invalidate(id)
  version_store.renew(cache_key(id), version_opts)
end
record_change(record, action) click to toggle source

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

cache_key(id) click to toggle source

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
fetch_records(query) click to toggle source
# File lib/record_cache/strategy/base.rb, line 50
def fetch_records(query)
  raise NotImplementedError
end
record_store() click to toggle source

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
statistics() click to toggle source

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
version_opts() click to toggle source

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
version_store() click to toggle source

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
versioned_key(cache_key, version) click to toggle source

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

with_multi_support(cache_store) click to toggle source

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