Module: Garner::Mixins::Grape::Cache
- Defined in:
- lib/garner/mixins/grape_cache.rb
Overview
A cache that supports conditional GETs
Borrows generously from themomorohoax.com/2009/01/07/using-stale-with-rails-to-return-304-not-modified See also RFC 2616: www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13.3.4
for explanation of how If-Modified-Since and If-None-Match request headers are handled.
Instance Method Summary collapse
-
#cache(options = {}, &block) ⇒ Object
cache a record.
- #cache_enabled? ⇒ Boolean
- #cache_or_304(options = {}, &block) ⇒ Object
-
#invalidate(*args) ⇒ Object
invalidate a cache record.
Instance Method Details
#cache(options = {}, &block) ⇒ Object
cache a record
18 19 20 21 22 23 24 25 26 27 |
# File 'lib/garner/mixins/grape_cache.rb', line 18 def cache( = {}, &block) unless cache_enabled? yield else binding, context = cache_binding_and_context() Garner::Cache::ObjectIdentity.cache(binding, context) do yield end end end |
#cache_enabled? ⇒ Boolean
13 14 15 |
# File 'lib/garner/mixins/grape_cache.rb', line 13 def cache_enabled? true end |
#cache_or_304(options = {}, &block) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/garner/mixins/grape_cache.rb', line 34 def cache_or_304( = {}, &block) unless cache_enabled? yield else binding, context = cache_binding_and_context() # metadata written in a previous GET = Garner::Cache::ObjectIdentity.(binding, context) error!("Not Modified", 304) if && fresh?() rc = cache(, &block) # metadata has been generated by cache = Garner::Cache::ObjectIdentity.(binding, context) if self.last_modified = [:last_modified] self.etag = [:etag] end rc end end |
#invalidate(*args) ⇒ Object
invalidate a cache record
30 31 32 |
# File 'lib/garner/mixins/grape_cache.rb', line 30 def invalidate(*args) Garner::Cache::ObjectIdentity.invalidate(* args) end |