Method: Dalli::Client#fetch
- Defined in:
- lib/dalli/client.rb
#fetch(key, ttl = nil, options = nil) ⇒ Object
Fetch the value associated with the key. If a value is found, then it is returned.
If a value is not found and no block is given, then nil is returned.
If a value is not found (or if the found value is nil and :cache_nils is false) and a block is given, the block will be invoked and its return value written to the cache and returned.
87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/dalli/client.rb', line 87 def fetch(key, ttl=nil, =nil) = .nil? ? CACHE_NILS : .merge(CACHE_NILS) if [:cache_nils] val = get(key, ) not_found = [:cache_nils] ? val == Dalli::Server::NOT_FOUND : val.nil? if not_found && block_given? val = yield add(key, val, ttl_or_default(ttl), ) end val end |