Class: RecordCache::Strategy::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/record_cache/strategy/base.rb

Direct Known Subclasses

FullTableCache, IndexCache, UniqueIndexCache

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base, attribute, record_store, options) ⇒ Base

Returns a new instance of Base.



10
11
12
13
14
15
16
# 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

Class Method Details

.parse(base, record_store, options) ⇒ Object

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

Raises:

  • (NotImplementedError)


6
7
8
# File 'lib/record_cache/strategy/base.rb', line 6

def self.parse(base, record_store, options)
  raise NotImplementedError
end

Instance Method Details

#attributeObject

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.



20
21
22
# File 'lib/record_cache/strategy/base.rb', line 20

def attribute
  @attribute
end

#cacheable?(query) ⇒ Boolean

Can the cache retrieve the records based on this query?

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


34
35
36
# File 'lib/record_cache/strategy/base.rb', line 34

def cacheable?(query)
  raise NotImplementedError
end

#fetch(query) ⇒ Object

Fetch all records and sort and filter locally



25
26
27
28
29
30
31
# 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) ⇒ Object

Handle invalidation call



44
45
46
# 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) ⇒ Object

Handle create/update/destroy (use record.previous_changes to find the old values in case of an update)

Raises:

  • (NotImplementedError)


39
40
41
# File 'lib/record_cache/strategy/base.rb', line 39

def record_change(record, action)
  raise NotImplementedError
end