Module: Harbor::ViewContext::Helpers::Cache
- Included in:
- Harbor::ViewContext
- Defined in:
- lib/harbor/view_context/helpers/cache.rb
Overview
Cache helper that provides fragment-caching
Defined Under Namespace
Classes: CacheRenderError
Instance Method Summary collapse
-
#cache(key, ttl = 30 * 60, max_age = nil, &generator) ⇒ Object
Caches the result of a block using the given TTL and maximum_age values.
Instance Method Details
#cache(key, ttl = 30 * 60, max_age = nil, &generator) ⇒ Object
Caches the result of a block using the given TTL and maximum_age values.
If no ttl is given, a default of 30 minutes is used. If no maximum_age value is given
the item will expire after Time.now + ttl. If a maximum_age is specified, "get" requests
to the cache for a given key will push the expiration time up for the item by the TTL, until
Time.now + TTL is equal to or greater than the cache-insertion-time + maximum_age.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/harbor/view_context/helpers/cache.rb', line 52 def cache(key, ttl = 30 * 60, max_age = nil, &generator) store = @cache_store || Harbor::View.cache if store.nil? raise ArgumentError.new("Cache Store Not Defined. Please set Harbor::View.cache to your desired cache store.") end content = if item = store.get(key) begin item.content rescue => e raise CacheRenderError.new(e, item) end else data = capture(&generator) store.put(key, data, ttl, max_age) data end with_buffer(generator) do |buffer| buffer << content end end |