module ResponseBank::Controller

Private Instance Methods

cache_age_tolerance_in_seconds() click to toggle source

If you're okay with serving pages that are not at the newest version, bump this up to whatever number of seconds you're comfortable with.

# File lib/response_bank/controller.rb, line 30
def cache_age_tolerance_in_seconds
  0
end
cache_key_data() click to toggle source
# File lib/response_bank/controller.rb, line 16
def cache_key_data
  { 'request' => { 'env' => request.env.slice('PATH_INFO', 'QUERY_STRING') } }
end
cache_version_data() click to toggle source

Override this method with additional information that changes to invalidate the cache.

# File lib/response_bank/controller.rb, line 12
def cache_version_data
  {}
end
cacheable_request?() click to toggle source

Only get? and head? requests should be cached.

# File lib/response_bank/controller.rb, line 7
def cacheable_request?
  (request.get? || request.head?) && (request.params[:cache] != 'false')
end
force_refill_cache?() click to toggle source
# File lib/response_bank/controller.rb, line 20
def force_refill_cache?
  params[:fill_cache] == "true"
end
response_cache(key_data = nil, version_data = nil) { || ... } click to toggle source
# File lib/response_bank/controller.rb, line 34
def response_cache(key_data = nil, version_data = nil, &block)
  cacheable_req = cacheable_request?

  unless cache_configured? && cacheable_req
    ResponseBank.log("Uncacheable request. cache_configured='#{!!cache_configured?}'" \
        " cacheable_request='#{cacheable_req}' params_cache='#{request.params[:cache] != 'false'}'")
    response.headers['Cache-Control'] = 'no-cache, no-store' unless cacheable_req
    return yield
  end

  handler = ResponseBank::ResponseCacheHandler.new(
    key_data: key_data || cache_key_data,
    version_data: version_data || cache_version_data,
    env: request.env,
    cache_age_tolerance: cache_age_tolerance_in_seconds,
    serve_unversioned: serve_unversioned_cacheable_entry?,
    force_refill_cache: force_refill_cache?,
    headers: response.headers,
    &block
  )

  status, _headers, body = handler.run!

  return if request.env['cacheable.miss']

  case request.env['cacheable.store']
  when 'server'
    render(status: status, plain: body.first)
  when 'client'
    head(:not_modified)
  end
end
serve_unversioned_cacheable_entry?() click to toggle source
# File lib/response_bank/controller.rb, line 24
def serve_unversioned_cacheable_entry?
  false
end