Module: Sinatra::MemCache::Helpers

Defined in:
lib/sinatra/memcache.rb

Instance Method Summary collapse

Instance Method Details

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



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/sinatra/memcache.rb', line 47

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



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/sinatra/memcache.rb', line 73

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