Module: WithTimedCache

Defined in:
lib/with_timed_cache.rb,
lib/with_timed_cache/cache.rb,
lib/with_timed_cache/caches.rb,
lib/with_timed_cache/version.rb,
lib/with_timed_cache/json_cache.rb,
lib/with_timed_cache/yaml_cache.rb

Defined Under Namespace

Classes: Cache, Caches, InvalidCacheFormatException, JSONCache, YAMLCache

Constant Summary collapse

VERSION =
"0.0.2"

Instance Method Summary collapse

Instance Method Details

#with_timed_cache(key, opts = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/with_timed_cache.rb', line 5

def with_timed_cache(key, opts = {})
  cache = Caches.find_or_create(key, opts)

  if cache.exists? && !cache.stale?
    data = cache.read
  else
    begin
      data = yield
      cache.write(data)
    rescue Exception => e
      data = cache.read rescue nil
    end
  end
  data
end