Class: Metanorma::Plugin::Lutaml::CacheStore
- Inherits:
-
Object
- Object
- Metanorma::Plugin::Lutaml::CacheStore
- Defined in:
- lib/metanorma/plugin/lutaml/cache_store.rb
Instance Method Summary collapse
- #clear ⇒ Object
- #fetch(key) ⇒ Object
- #fetch_or_store(key) ⇒ Object
-
#initialize(max_size: 50) ⇒ CacheStore
constructor
A new instance of CacheStore.
- #size ⇒ Object
- #store(key, value) ⇒ Object
Constructor Details
#initialize(max_size: 50) ⇒ CacheStore
Returns a new instance of CacheStore.
7 8 9 10 11 |
# File 'lib/metanorma/plugin/lutaml/cache_store.rb', line 7 def initialize(max_size: 50) @store = {} @max_size = max_size @mutex = Mutex.new end |
Instance Method Details
#clear ⇒ Object
35 36 37 |
# File 'lib/metanorma/plugin/lutaml/cache_store.rb', line 35 def clear @mutex.synchronize { @store.clear } end |
#fetch(key) ⇒ Object
13 14 15 |
# File 'lib/metanorma/plugin/lutaml/cache_store.rb', line 13 def fetch(key) @mutex.synchronize { @store[key] } end |
#fetch_or_store(key) ⇒ Object
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/metanorma/plugin/lutaml/cache_store.rb', line 24 def fetch_or_store(key) @mutex.synchronize do if @store.key?(key) @store[key] else evict! if @store.size >= @max_size @store[key] = yield end end end |
#size ⇒ Object
39 40 41 |
# File 'lib/metanorma/plugin/lutaml/cache_store.rb', line 39 def size @mutex.synchronize { @store.size } end |
#store(key, value) ⇒ Object
17 18 19 20 21 22 |
# File 'lib/metanorma/plugin/lutaml/cache_store.rb', line 17 def store(key, value) @mutex.synchronize do evict! if @store.size >= @max_size @store[key] = value end end |