Class: Sinatra::TemplateCache
- Inherits:
-
Object
- Object
- Sinatra::TemplateCache
- 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.
Instance Method Summary collapse
-
#clear ⇒ Object
Clears the cache.
-
#fetch(*key) ⇒ Object
Caches a value for key, or returns the previously cached value.
-
#initialize ⇒ TemplateCache
constructor
A new instance of TemplateCache.
Constructor Details
#initialize ⇒ TemplateCache
Returns a new instance of TemplateCache.
949 950 951 |
# File 'lib/sinatra/base.rb', line 949 def initialize @cache = {} end |
Instance Method Details
#clear ⇒ Object
Clears the cache.
964 965 966 |
# File 'lib/sinatra/base.rb', line 964 def clear @cache = {} end |
#fetch(*key) ⇒ Object
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.
957 958 959 960 961 |
# File 'lib/sinatra/base.rb', line 957 def fetch(*key) @cache.fetch(key) do @cache[key] = yield end end |