Class: RecordCache::Strategy::Base
- Inherits:
-
Object
- Object
- RecordCache::Strategy::Base
- Defined in:
- lib/record_cache/strategy/base.rb
Direct Known Subclasses
Class Method Summary collapse
-
.parse(base, record_store, options) ⇒ Object
Parse the options and return (an array of) instances of this strategy.
Instance Method Summary collapse
-
#attribute ⇒ Object
Retrieve the
attribute
for this strategy (unique per model). -
#cacheable?(query) ⇒ Boolean
Can the cache retrieve the records based on this query?.
-
#fetch(query) ⇒ Object
Fetch all records and sort and filter locally.
-
#initialize(base, attribute, record_store, options) ⇒ Base
constructor
A new instance of Base.
-
#invalidate(id) ⇒ Object
Handle invalidation call.
-
#record_change(record, action) ⇒ Object
Handle create/update/destroy (use record.previous_changes to find the old values in case of an update).
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, ) @base = base @attribute = attribute @record_store = with_multi_support(record_store) @cache_key_prefix = "rc/#{[:key] || @base.name}/" @version_opts = [:ttl] ? { :ttl => [:ttl] } : {} end |
Class Method Details
.parse(base, record_store, options) ⇒ Object
Parse the options and return (an array of) instances of this strategy.
6 7 8 |
# File 'lib/record_cache/strategy/base.rb', line 6 def self.parse(base, record_store, ) raise NotImplementedError end |
Instance Method Details
#attribute ⇒ Object
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?
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)
39 40 41 |
# File 'lib/record_cache/strategy/base.rb', line 39 def record_change(record, action) raise NotImplementedError end |