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

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(options = {}, &block)
  unless cache_enabled?
    yield
  else
    binding, context = cache_binding_and_context(options)
    Garner::Cache::ObjectIdentity.cache(binding, context) do
      yield
    end
  end
end

#cache_enabled?Boolean

Returns:

  • (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(options = {}, &block)
  unless cache_enabled?
    yield
  else
    binding, context = cache_binding_and_context(options)
    # metadata written in a previous GET
     = Garner::Cache::ObjectIdentity.(binding, context)
    error!("Not Modified", 304) if  && fresh?()
    rc = cache(options, &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