Method: Cash::Accessor::ClassMethods#fetch

Defined in:
lib/cash/accessor.rb

#fetch(keys, options = {}, &block) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/cash/accessor.rb', line 11

def fetch(keys, options = {}, &block)
  case keys
  when Array
    return {} if keys.empty?
    
    keys = keys.collect { |key| cache_key(key) }
    hits = repository.get_multi(*keys)
    if (missed_keys = keys - hits.keys).any?
      missed_values = block.call(missed_keys)
      hits.merge!(missed_keys.zip(Array(missed_values)).to_hash_without_nils)
    end
    hits
  else
    repository.get(cache_key(keys), options[:raw]) || (block ? block.call : nil)
  end
end