Class: SmartCollection::CacheManager::CacheStore
Instance Method Summary
collapse
determine_class, #expires_in
Constructor Details
#initialize(model:, config:) ⇒ CacheStore
Returns a new instance of CacheStore.
5
6
7
8
9
|
# File 'lib/smart_collection/cache_manager/cache_store.rb', line 5
def initialize model:, config:
super
@cache_key_proc = @config.cache_config[:cache_key_proc] || -> (owner) { "_smart_collection_#{owner.class.name}_#{owner.id}" }
end
|
Instance Method Details
#cache_exists?(owner) ⇒ Boolean
36
37
38
|
# File 'lib/smart_collection/cache_manager/cache_store.rb', line 36
def cache_exists? owner
owner.cache_expires_at && owner.cache_expires_at > Time.now && cache_store.exist?(cache_key owner)
end
|
#cache_key(owner) ⇒ Object
15
16
17
|
# File 'lib/smart_collection/cache_manager/cache_store.rb', line 15
def cache_key owner
@cache_key_proc.(owner)
end
|
#cache_store ⇒ Object
11
12
13
|
# File 'lib/smart_collection/cache_manager/cache_store.rb', line 11
def cache_store
@config.cache_config[:cache_store]
end
|
#read_multi(owners) ⇒ Object
28
29
30
31
32
33
34
|
# File 'lib/smart_collection/cache_manager/cache_store.rb', line 28
def read_multi owners
cache_keys = owners.map{|owner| cache_key owner}
loaded = cache_store.read_multi(*cache_keys)
owners.map.with_index do |owner, index|
[owner, Marshal.load(loaded[cache_keys[index]])]
end.to_h
end
|
#read_scope(owner) ⇒ Object
24
25
26
|
# File 'lib/smart_collection/cache_manager/cache_store.rb', line 24
def read_scope owner
@config.item_class.where(id: Marshal.load(cache_store.read(cache_key owner)))
end
|
#update(owner) ⇒ Object
19
20
21
22
|
# File 'lib/smart_collection/cache_manager/cache_store.rb', line 19
def update owner
cache_store.write(cache_key(owner), Marshal.dump(owner.smart_collection_mixin.uncached_scope(owner).pluck(:id)))
owner.update(cache_expires_at: Time.now + expires_in)
end
|