Module: Sinatra::MemCache::Helpers

Defined in:
lib/sinatra/memcache.rb

Instance Method Summary collapse

Instance Method Details

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



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

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

  opts = {
    :expiry => options.cache_default_expiry,
    :compress => options.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 development?
  block.call
end

#expire(p) ⇒ Object



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

def expire(p)
  return unless options.cache_enable

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