Module: Sinatra::Dalli::Helpers

Defined in:
lib/sinatra/dalli.rb

Instance Method Summary collapse

Instance Method Details

#cache(key, params = {}, &block) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/sinatra/dalli.rb', line 14

def cache(key, params = {}, &block)
  return block.call unless settings.cache_enable

  opts = {
    :expiry => settings.cache_default_expiry,
    :compress => settings.cache_default_compress
  }.merge(params)

  value = get(key, opts)
  return value unless block_given?

  if value
    log "Get: #{key}"
    value
  else
    log "Set: #{key}"
    set(key, block.call, opts)
  end
rescue => e
  throw e if settings.development? || settings.show_exceptions
  block.call
end

#expire(p) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/sinatra/dalli.rb', line 40

def expire(p)
  return unless settings.cache_enable

  case p
  when String
    expire_key(p)
  when Regexp
    expire_regexp(p)
  end
  true
rescue => e
  throw e if settings.development? or settings.show_exceptions
  false
end