Module: Sinatra::SimpleCache
- Defined in:
- lib/sinatra/simplecache.rb,
lib/sinatra/simplecache/version.rb
Constant Summary collapse
- VERSION =
"0.1.0"
Instance Method Summary collapse
-
#cache(opts = {}) { ... } ⇒ String
Block evaluated value, or cached value.
Instance Method Details
#cache(opts = {}) { ... } ⇒ String
Returns Block evaluated value, or cached value.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/sinatra/simplecache.rb', line 24 def cache(opts={}, &block) @@__entries__ ||= {} now = Time.now.to_f (expire = opts[:expire]) ? (expire + now) : (@@inf ||= 1/0.0) key = opts[:key] || (defined?(Sinatra) && request.path) || abort("set key") if (e = @@__entries__[key]) && (e[:expire] > now) return e[:value] end value = block.call @@__entries__[key] = { expire: expire, value: value } value end |