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.

The headers to set in development and production 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
    

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" }

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 produdction mode

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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of AutoCaching.



42
43
44
45
46
47
# File 'lib/startback/web/auto_caching.rb', line 42

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



49
50
51
52
# File 'lib/startback/web/auto_caching.rb', line 49

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