Class: LeveledCache::Store
- Inherits:
-
ActiveSupport::Cache::Store
- Object
- ActiveSupport::Cache::Store
- LeveledCache::Store
- Defined in:
- lib/leveled_cache/store.rb
Overview
A cache store implementation that acts as a proxy over multiple caches (“levels”) that are called in order. Caches specified earlier are checked for values earlier.
A typical use-case is to put a fast, in-process MemoryStore in front of a larger, slower RedisCacheStore:
cache = LeveledCache::Store.new(
ActiveSupport::Cache::MemoryStore.new,
ActiveSupport::Cache::RedisCacheStore.new,
)
All interactions use the underlying caches’ public APIs, which means an interaction on the LeveledProxy potentially serialized, compressed, etc. multiple times as the interaction progresses through levels. Take care to configure underlying caches accordingly. For example, it may be desirable to use NullCoder on an earlier MemoryStore.
Instance Method Summary collapse
-
#delete(name, **options) ⇒ Object
Deletes the value from all cache levels with the provided key.
-
#fetch(name, **options) ⇒ Object
Fetches data from the caches in order using the given key.
-
#fetch_multi(*names, **options) ⇒ Object
Fetches data from the caches in order using the given keys.
-
#initialize(*caches) ⇒ Store
constructor
Create a new LeveledCacheProxy that calls cache methods on one or more underlying caches in order.
-
#read(name, **options) ⇒ Object
Reads data from the caches in order using the given key.
-
#read_multi(*names, **options) ⇒ Object
Reads data from the caches in order using the given keys.
-
#write(name, value, **options) ⇒ Object
Writes the value to all cache levels with the provided key.
-
#write_multi(hash, **options) ⇒ Object
Writes the key value pairs to all cache levels.
Constructor Details
#initialize(*caches) ⇒ Store
Create a new LeveledCacheProxy that calls cache methods on one or more underlying caches in order.
Options
No options are allowed. Set options on the caches provided.
32 33 34 35 |
# File 'lib/leveled_cache/store.rb', line 32 def initialize(*caches) @caches = caches = {} end |
Instance Method Details
#delete(name, **options) ⇒ Object
Deletes the value from all cache levels with the provided key.
Options are passed through to the underlying caches.
92 93 94 |
# File 'lib/leveled_cache/store.rb', line 92 def delete(name, **) delete_entry(name, **) end |
#fetch(name, **options) ⇒ Object
Fetches data from the caches in order using the given key. Data is returned from the first cache with data for the given key.
Internally, fetch is called on each cache level recursively until data is found. If data is missing from “earlier” caches but found in “later” caches, all “earlier” caches will populate with the found data.
If no data is found in any caches, the block is called and all caches are populated with the return value.
Options are passed through to the underlying caches.
49 50 51 |
# File 'lib/leveled_cache/store.rb', line 49 def fetch(name, **, &) _fetch(@caches, name, , &) end |
#fetch_multi(*names, **options) ⇒ Object
Fetches data from the caches in order using the given keys. Data is returned from the first cache with data for the given keys.
If no data is found in any caches, the block is called and all caches are populated with the return value.
Options are passed through to the underlying caches.
60 61 62 |
# File 'lib/leveled_cache/store.rb', line 60 def fetch_multi(*names, **, &) _fetch_multi(@caches, names, , &) end |
#read(name, **options) ⇒ Object
Reads data from the caches in order using the given key. Data is returned from the first cache level with data for the given key. Otherwise, nil is returned.
Options are passed through to the underlying caches.
69 70 71 |
# File 'lib/leveled_cache/store.rb', line 69 def read(name, **) read_entry(name, **) end |
#read_multi(*names, **options) ⇒ Object
Reads data from the caches in order using the given keys. Data is returned from the first cache level with data for a key. Otherwise, {} is returned.
Options are passed through to the underlying caches.
78 79 80 |
# File 'lib/leveled_cache/store.rb', line 78 def read_multi(*names, **) read_multi_entries(names, **) end |
#write(name, value, **options) ⇒ Object
Writes the value to all cache levels with the provided key.
Options are passed through to the underlying caches.
85 86 87 |
# File 'lib/leveled_cache/store.rb', line 85 def write(name, value, **) write_entry(name, value, **) end |
#write_multi(hash, **options) ⇒ Object
Writes the key value pairs to all cache levels.
Options are passed through to the underlying caches.
99 100 101 |
# File 'lib/leveled_cache/store.rb', line 99 def write_multi(hash, **) write_multi_entries(hash, **) end |