Class: Phlex::FIFOCacheStore
- Inherits:
-
Object
- Object
- Phlex::FIFOCacheStore
- Defined in:
- lib/phlex/fifo_cache_store.rb
Overview
An extremely fast in-memory cache store that evicts keys on a first-in-first-out basis.
Instance Method Summary collapse
- #clear ⇒ Object
- #fetch(key) ⇒ Object
-
#initialize(max_bytesize: 2 ** 20) ⇒ FIFOCacheStore
constructor
A new instance of FIFOCacheStore.
Constructor Details
#initialize(max_bytesize: 2 ** 20) ⇒ FIFOCacheStore
Returns a new instance of FIFOCacheStore.
5 6 7 8 9 10 |
# File 'lib/phlex/fifo_cache_store.rb', line 5 def initialize(max_bytesize: 2 ** 20) @fifo = Phlex::FIFO.new( max_bytesize:, max_value_bytesize: max_bytesize ) end |
Instance Method Details
#clear ⇒ Object
27 28 29 |
# File 'lib/phlex/fifo_cache_store.rb', line 27 def clear @fifo.clear end |
#fetch(key) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/phlex/fifo_cache_store.rb', line 12 def fetch(key) fifo = @fifo key = map_key(key) if (result = fifo[key]) JSON.parse(result) else result = yield fifo[key] = JSON.fast_generate(result) result end end |