Method: Cachy.cache

Defined in:
lib/cachy.rb

.cache(*args) ⇒ Object

Cache the result of a block

Cachy.cache(:my_key){ expensive() } Cachy.cache(:my_key, :expires_in=>1.hour){ expensive() } Cachy.cache(:my_key, :keys=>){ expensive() } Cachy.cache(:my_key, :without_locale=>true){ expensive() } Cachy.cache(:my_key, :hash_key=>true){ expensive() }



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

def self.cache(*args)
  key = key(*args)
  options = extract_options!(args)

  # Cached result?
  result = cache_store.read(key)
  return result unless result == nil

  # Calculate result!
  set_while_running(key, options)

  result = yield
  cache_store.write key, result, options
  result
end