Method: SkullIsland::LRUCache#retrieve

Defined in:
lib/skull_island/lru_cache.rb

#retrieve(key) ⇒ Object Also known as: []

Retrieve an item from the cache. Returns ‘nil` if the item does not exist. Relies on #store returning the stored value to ensure the LRU algorithm is maintained safely.

Parameters:

  • key (Symbol)

    the index to retrieve



147
148
149
150
151
152
153
154
155
156
# File 'lib/skull_island/lru_cache.rb', line 147

def retrieve(key)
  if has?(key)
    @meta_mutex.synchronize { @hits += 1 }
    # Looks dumb, but it actually only reorganizes the keys Array
    store(key, @read_mutex.synchronize { @data[key] })
  else
    @meta_mutex.synchronize { @misses += 1 }
    nil
  end
end