Class: Sinatra::TemplateCache
Relationships & Source Files | |
Inherits: | Object |
Defined in: | lib/sinatra/base.rb |
Overview
Extremely simple template cache implementation.
- Not thread-safe.
- Size is unbounded.
- Keys are not copied defensively, and should not be modified after being passed to #fetch. More specifically, the values returned by key#hash and key#eql? should not change.
Implementation copied from Tilt::Cache.
Class Method Summary
- .new ⇒ TemplateCache constructor
Instance Method Summary
-
#clear
Clears the cache.
-
#fetch(*key)
Caches a value for key, or returns the previously cached value.
Constructor Details
.new ⇒ TemplateCache
# File 'lib/sinatra/base.rb', line 952
def initialize @cache = {} end
Instance Method Details
#clear
Clears the cache.
# File 'lib/sinatra/base.rb', line 967
def clear @cache = {} end
#fetch(*key)
Caches a value for key, or returns the previously cached value. If a value has been previously cached for key then it is returned. Otherwise, block is yielded to and its return value which may be nil, is cached under key and returned.
# File 'lib/sinatra/base.rb', line 960
def fetch(*key) @cache.fetch(key) do @cache[key] = yield end end