Class: Looksist::SafeLruCache

Inherits:
Hash
  • Object
show all
Includes:
MonitorMixin
Defined in:
lib/looksist/safe_lru_cache.rb

Instance Method Summary collapse

Methods inherited from Hash

#find_all_values_for

Constructor Details

#initialize(max_size) ⇒ SafeLruCache

Returns a new instance of SafeLruCache.



8
9
10
11
# File 'lib/looksist/safe_lru_cache.rb', line 8

def initialize(max_size)
  @max_size = max_size
  super(nil)
end

Instance Method Details

#[]=(key, val) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/looksist/safe_lru_cache.rb', line 13

def []=(key, val)
  synchronize do
    super(key, val)
    pop
    val
  end
end

#merge!(hash) ⇒ Object



21
22
23
24
25
26
# File 'lib/looksist/safe_lru_cache.rb', line 21

def merge!(hash)
  synchronize do
    super(hash)
    (count - @max_size).times { pop }
  end
end

#mslice(keys) ⇒ Object



28
29
30
31
32
# File 'lib/looksist/safe_lru_cache.rb', line 28

def mslice(keys)
  synchronize do
    keys.collect { |k| self[k] }
  end
end