class Startback::Web::AutoCaching
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" }
Constants
- DEVELOPMENT_CACHE_CONTROL
Cache-Control header to use in development mode
- PRODUCTION_CACHE_CONTROL
Cache-Control header to use in produdction mode
Public Class Methods
new(app, development = nil, cache_headers = {})
click to toggle source
# 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
Public Instance Methods
call(env)
click to toggle source
# 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
Protected Instance Methods
default_headers()
click to toggle source
# File lib/startback/web/auto_caching.rb, line 68 def default_headers { development: { "Cache-Control" => DEVELOPMENT_CACHE_CONTROL }, production: { "Cache-Control" => PRODUCTION_CACHE_CONTROL } } end
development?()
click to toggle source
# File lib/startback/web/auto_caching.rb, line 60 def development? !!@development end
infer_is_development()
click to toggle source
# File lib/startback/web/auto_caching.rb, line 64 def infer_is_development ENV['RACK_ENV'] != "production" end
normalize_headers(h)
click to toggle source
# File lib/startback/web/auto_caching.rb, line 79 def normalize_headers(h) Hash[h.map{|k,v| [k, v.is_a?(Hash) ? v : {"Cache-Control" => v} ] }] end
patch_response_headers(hs)
click to toggle source
# File lib/startback/web/auto_caching.rb, line 56 def patch_response_headers(hs) (development? ? @cache_headers[:development] : @cache_headers[:production]).merge(hs) end