Class: LHC::Caching

Inherits:
Interceptor show all
Includes:
ActiveSupport::Configurable
Defined in:
lib/lhc/interceptors/caching.rb

Constant Summary collapse

CACHE_VERSION =
'1'
FORWARDED_OPTIONS =

Options forwarded to the cache

[:expires_in, :race_condition_ttl]

Instance Attribute Summary

Attributes inherited from Interceptor

#request

Instance Method Summary collapse

Methods inherited from Interceptor

#after_request, #before_raw_request, #before_response, dup, #initialize, #response

Constructor Details

This class inherits a constructor from LHC::Interceptor

Instance Method Details

#after_responseObject


24
25
26
27
28
29
30
31
32
33
34
# File 'lib/lhc/interceptors/caching.rb', line 24

def after_response
  return unless response.success?
  request = response.request
  return unless cache?(request)
  options = options(request.options)
  cache_for(options).write(
    key(request, options[:key]),
    to_cache(response),
    cache_options(options)
  )
end

#before_requestObject


13
14
15
16
17
18
19
20
21
22
# File 'lib/lhc/interceptors/caching.rb', line 13

def before_request
  return unless cache?(request)
  deprecation_warning(request.options)
  options = options(request.options)
  key = key(request, options[:key])
  response_data = cache_for(options).fetch(key)
  return unless response_data
  logger&.info "Served from cache: #{key}"
  from_cache(request, response_data)
end