Class: Metanorma::Plugin::Lutaml::CacheStore

Inherits:
Object
  • Object
show all
Defined in:
lib/metanorma/plugin/lutaml/cache_store.rb

Instance Method Summary collapse

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

#clearObject



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

#sizeObject



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