Module: Hanami::Action::Cache

Defined in:
lib/hanami/action/cache.rb,
lib/hanami/action/cache/expires.rb,
lib/hanami/action/cache/directives.rb,
lib/hanami/action/cache/cache_control.rb,
lib/hanami/action/cache/conditional_get.rb

Overview

Cache type API

See Also:

  • ClassMethods#cache_control
  • ClassMethods#expires
  • ClassMethods#fresh

Since:

  • 0.3.0

Defined Under Namespace

Modules: CacheControl, Expires Classes: ConditionalGet, Directives, ETag, LastModified, NonValueDirective, ValueDirective

Constant Summary collapse

VALUE_DIRECTIVES =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Cache-Control directives which have values

Since:

  • 0.3.0

max_age s_maxage min_fresh max_stale).freeze
NON_VALUE_DIRECTIVES =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Cache-Control directives which are implicitly true

Since:

  • 0.3.0

public private no_cache no_store no_transform must_revalidate proxy_revalidate).freeze
IF_NONE_MATCH =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Since:

  • 0.3.0

'HTTP_IF_NONE_MATCH'.freeze
ETAG =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

The HTTP header for ETag

Since:

  • 0.3.0

'ETag'.freeze
IF_MODIFIED_SINCE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Since:

  • 0.3.0

'HTTP_IF_MODIFIED_SINCE'.freeze
LAST_MODIFIED =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

The HTTP header for Last-Modified

Since:

  • 0.3.0

'Last-Modified'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Override Ruby's hook for modules. It includes exposures logic

Parameters:

  • base (Class)

    the target action

See Also:

Since:

  • 0.3.0


24
25
26
27
28
# File 'lib/hanami/action/cache.rb', line 24

def self.included(base)
  base.class_eval do
    include CacheControl, Expires
  end
end

Instance Method Details

#cache_control(*values) ⇒ Object (protected)

Specify response freshness policy for HTTP caches (Cache-Control header). Any number of non-value directives (:public, :private, :no_cache, :no_store, :must_revalidate, :proxy_revalidate) may be passed along with a Hash of value directives (:max_age, :min_stale, :s_max_age).

See RFC 2616 / 14.9 for more on standard cache control directives: tools.ietf.org/html/rfc2616#section-14.9.1

Examples:

require 'hanami/controller'
require 'hanami/action/cache'

class Show
  include Hanami::Action
  include Hanami::Action::Cache

  def call(params)
    # ...

    # set Cache-Control directives
    cache_control :public, max_age: 900, s_maxage: 86400

    # overwrite previous Cache-Control directives
    cache_control :private, :no_cache, :no_store

    => Cache-Control: private, no-store, max-age=900

  end
end

Parameters:

  • values (Array<Symbols, Hash>)

    mapped to cache_control directives

Options Hash (*values):

  • :public (Symbol)
  • :private (Symbol)
  • :no_cache (Symbol)
  • :no_store (Symbol)
  • :must_validate (Symbol)
  • :proxy_revalidate (Symbol)
  • :max_age (Hash)
  • :min_stale (Hash)
  • :s_max_age (Hash)

Returns:

  • void

Since:

  • 0.3.0


78
79
80
81
# File 'lib/hanami/action/cache.rb', line 78

def cache_control(*values)
  cache_control = CacheControl::Directives.new(*values)
  headers.merge!(cache_control.headers)
end

#expires(amount, *values) ⇒ Object (protected)

Set the Expires header and Cache-Control/max-age directive. Amount can be an integer number of seconds in the future or a Time object indicating when the response should be considered “stale”. The remaining “values” arguments are passed to the #cache_control helper:

Examples:

require 'hanami/controller'
require 'hanami/action/cache'

class Show
  include Hanami::Action
  include Hanami::Action::Cache

  def call(params)
    # ...

    # set Cache-Control directives and Expires
    expires 900, :public

    # overwrite Cache-Control directives and Expires
    expires 300, :private, :no_cache, :no_store

    => Expires: Thu, 26 Jun 2014 12:00:00 GMT
    => Cache-Control: private, no-cache, no-store max-age=300

  end
end

Parameters:

  • amount (Integer, Time)

    number of seconds or point in time

  • values (Array<Symbols>)

    mapped to cache_control directives

Returns:

  • void

Since:

  • 0.3.0


119
120
121
122
# File 'lib/hanami/action/cache.rb', line 119

def expires(amount, *values)
  expires = Expires::Directives.new(amount, *values)
  headers.merge!(expires.headers)
end

#fresh(options) ⇒ Object (protected)

Set the etag, last_modified, or both headers on the response and halts a 304 Not Modified if the request is still fresh respecting IfNoneMatch and IfModifiedSince request headers

Examples:

require 'hanami/controller'
require 'hanami/action/cache'

class Show
  include Hanami::Action
  include Hanami::Action::Cache

  def call(params)
    # ...

    # set etag response header and halt 304
    # if request matches IF_NONE_MATCH header
    fresh etag: @resource.updated_at.to_i

    # set last_modified response header and halt 304
    # if request matches IF_MODIFIED_SINCE
    fresh last_modified: @resource.updated_at

    # set etag and last_modified response header,
    # halt 304 if request matches IF_MODIFIED_SINCE
    # and IF_NONE_MATCH
    fresh last_modified: @resource.updated_at

  end
end

Parameters:

  • options (Hash)

Options Hash (options):

  • :etag (Integer)

    for testing IfNoneMatch conditions

  • :last_modified (Date)

    for testing IfModifiedSince conditions

Returns:

  • void

Since:

  • 0.3.0


163
164
165
166
167
168
169
170
171
# File 'lib/hanami/action/cache.rb', line 163

def fresh(options)
  conditional_get = ConditionalGet.new(@_env, options)

  headers.merge!(conditional_get.headers)

  conditional_get.fresh? do
    halt 304
  end
end