Class: Phlex::FIFOCacheStore

Inherits:
Object
  • Object
show all
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

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

#clearObject



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