Method: FastCache::Cache#fetch

Defined in:
lib/fast_cache/cache.rb

#fetch(key) { ... } ⇒ Object

Retrieves a value from the cache, if available and not expired, or yields to a block that calculates the value to be stored in the cache.

Parameters:

  • key (Object)

    the key to look up or store at

Yields:

  • yields when the value is not present

Yield Returns:

  • (Object)

    the value to store in the cache.

Returns:

  • (Object)

    the value at the key



51
52
53
54
55
56
57
58
# File 'lib/fast_cache/cache.rb', line 51

def fetch(key)
  found, value = get(key)
  if found
    value
  else
    store(key, yield)
  end
end