Module: SpreeCmCommissioner::ContentCachable

Extended by:
ActiveSupport::Concern
Defined in:
app/controllers/concerns/spree_cm_commissioner/content_cachable.rb

Instance Method Summary collapse

Instance Method Details

#max_ageObject

Override This Method based on your UseCase



10
11
12
# File 'app/controllers/concerns/spree_cm_commissioner/content_cachable.rb', line 10

def max_age
  ENV.fetch('CONTENT_CACHE_MAX_AGE', 1.day.to_i).to_i
end

#set_cache_control_for_cdnObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/controllers/concerns/spree_cm_commissioner/content_cachable.rb', line 14

def set_cache_control_for_cdn
  return if response.committed?
  return if response.status != 200

  # max-age: browser/client cache, s-maxage: CDN/server cache
  # Allow client caching for mobile apps while keeping CDN cache longer
  client_max_age = [max_age / 2, 300].max # At least 5 minutes, or half of CDN cache
  response.headers['Cache-Control'] = "public, max-age=#{client_max_age}, s-maxage=#{max_age}"

  # Pragma: HTTP/1.0 cache directive (predates Cache-Control).
  # 'cache' allows caching.
  response.headers['Pragma'] = 'cache'

  # Expires: Absolute timestamp when cache becomes stale (HTTP/1.0 fallback)
  response.headers['Expires'] = (Time.zone.now + client_max_age).httpdate
end