Class: Startback::Web::AutoCaching

Inherits:
Object
  • Object
show all
Defined in:
lib/startback/web/auto_caching.rb

Overview

This rack middleware automatically mark response as non being cacheable in development, and being cacheble in production.

An error response is never made cacheable, in either mode. Whether a request is answered 4xx usually depends on that very request -- a header, a cookie, a credential -- while a cache in front of the application keys on the url alone, so one client's error would otherwise be served to everyone asking for the url next. It is marked no-store rather than merely left alone, because 404 and 410 are heuristically cacheable (RFC 9111) and a shared cache may keep them with no header to go by.

The headers to set in development, in production and on an error can be passed at construction, as well as whether the development environment must be forced. This class may also be configured through environment variables:

  • RACK_ENV: when "production" use the production headers, otherwise use the development ones
  • STARTBACK_AUTOCACHING_DEVELOPMENT_CACHE_CONTROL: Cache-Control header to use in development mode
  • STARTBACK_AUTOCACHING_PRODUCTION_CACHE_CONTROL: Cache-Control header to use in production mode
  • STARTBACK_AUTOCACHING_ERROR_CACHE_CONTROL: Cache-Control header to use on a response of status 400 and up

Example:

# Default configuration
use Autocaching

# Force development mode
use Autocaching, true

# Force production mode
use Autocaching, false

# Set production headers manually
use Autocaching, { :production => "public, no-cache, no-store" }

# Let a shared cache keep a not found for a minute
use Autocaching, { :error => "public, max-age=60" }

Constant Summary collapse

DEVELOPMENT_CACHE_CONTROL =

Cache-Control header to use in development mode

ENV['STARTBACK_AUTOCACHING_DEVELOPMENT_CACHE_CONTROL'] || \
"no-cache, no-store, max-age=0, must-revalidate"
PRODUCTION_CACHE_CONTROL =

Cache-Control header to use in production mode. s-maxage, and not s-max-age: the latter is not a directive any cache knows, so it was silently dropped and a shared cache had nothing but max-age to go by -- the very knob an application needs to keep a page in its reverse proxy for an hour while the browser comes back in a minute.

ENV['STARTBACK_AUTOCACHING_PRODUCTION_CACHE_CONTROL'] ||\
"public, must-revalidate, max-age=3600, s-maxage=3600"
ERROR_CACHE_CONTROL =

Cache-Control header to use on an error response, in either mode

ENV['STARTBACK_AUTOCACHING_ERROR_CACHE_CONTROL'] ||\
"no-store"
ERROR_STATUS =

Lowest status this middleware considers an error

400

Instance Method Summary collapse

Constructor Details

#initialize(app, development = nil, cache_headers = {}) ⇒ AutoCaching

Returns a new instance of AutoCaching.



69
70
71
72
73
74
# File 'lib/startback/web/auto_caching.rb', line 69

def initialize(app, development = nil, cache_headers = {})
  development, cache_headers = nil, development if development.is_a?(Hash)
  @app = app
  @development = development.nil? ? infer_is_development : development
  @cache_headers = default_headers.merge(normalize_headers(cache_headers))
end

Instance Method Details

#call(env) ⇒ Object



76
77
78
79
# File 'lib/startback/web/auto_caching.rb', line 76

def call(env)
  status, headers, body = @app.call(env)
  [status, patch_response_headers(status, headers), body]
end