123456789_123456789_123456789_123456789_123456789_

Class: ActionDispatch::Http::Cache::Request::CacheControlDirectives

Relationships & Source Files
Inherits: Object
Defined in: actionpack/lib/action_dispatch/http/cache.rb

Overview

Represents the HTTP Cache-Control header for requests, providing methods to access various cache control directives Reference: www.rfc-editor.org/rfc/rfc9111.html#name-request-directives

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(cache_control_header) ⇒ CacheControlDirectives

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/http/cache.rb', line 75

def initialize(cache_control_header)
  @only_if_cached = false
  @no_cache = false
  @no_store = false
  @no_transform = false
  @max_age = nil
  @max_stale = nil
  @min_fresh = nil
  @stale_if_error = false
  parse_directives(cache_control_header)
end

Instance Attribute Details

#max_age (readonly)

Returns the value of the max-age directive. This directive indicates that the client is willing to accept a response whose age is no greater than the specified number of seconds.

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/http/cache.rb', line 118

attr_reader :max_age

#max_stale (readonly)

Returns the value of the max-stale directive. When max-stale is present with a value, returns that integer value. When max-stale is present without a value, returns true (unlimited staleness). When max-stale is not present, returns nil.

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/http/cache.rb', line 124

attr_reader :max_stale

#max_stale?Boolean (readonly)

Returns true if max-stale directive is present (with or without a value)

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/http/cache.rb', line 127

def max_stale?
  !@max_stale.nil?
end

#max_stale_unlimited?Boolean (readonly)

Returns true if max-stale directive is present without a value (unlimited staleness)

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/http/cache.rb', line 132

def max_stale_unlimited?
  @max_stale == true
end

#min_fresh (readonly)

Returns the value of the min-fresh directive. This directive indicates that the client is willing to accept a response whose freshness lifetime is no less than its current age plus the specified time in seconds.

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/http/cache.rb', line 139

attr_reader :min_fresh

#no_cache?Boolean (readonly)

Returns true if the no-cache directive is present. This directive indicates that a cache must not use the response to satisfy subsequent requests without successful validation on the origin server.

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/http/cache.rb', line 98

def no_cache?
  @no_cache
end

#no_store?Boolean (readonly)

Returns true if the no-store directive is present. This directive indicates that a cache must not store any part of the request or response.

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/http/cache.rb', line 105

def no_store?
  @no_store
end

#no_transform?Boolean (readonly)

Returns true if the no-transform directive is present. This directive indicates that a cache or proxy must not transform the payload.

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/http/cache.rb', line 111

def no_transform?
  @no_transform
end

#only_if_cached?Boolean (readonly)

Returns true if the only-if-cached directive is present. This directive indicates that the client only wishes to obtain a stored response. If a valid stored response is not available, the server should respond with a 504 (Gateway Timeout) status.

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/http/cache.rb', line 91

def only_if_cached?
  @only_if_cached
end

#stale_if_error (readonly)

Returns the value of the stale-if-error directive. This directive indicates that the client is willing to accept a stale response if the check for a fresh one fails with an error for the specified number of seconds.

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/http/cache.rb', line 144

attr_reader :stale_if_error

Instance Method Details

#parse_directives(header_value) (private)

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/http/cache.rb', line 147

def parse_directives(header_value)
  return unless header_value

  header_value.delete(" ").downcase.split(",").each do |directive|
    name, value = directive.split("=", 2)

    case name
    when "max-age"
      @max_age = value.to_i
    when "min-fresh"
      @min_fresh = value.to_i
    when "stale-if-error"
      @stale_if_error = value.to_i
    when "no-cache"
      @no_cache = true
    when "no-store"
      @no_store = true
    when "no-transform"
      @no_transform = true
    when "only-if-cached"
      @only_if_cached = true
    when "max-stale"
      @max_stale = value ? value.to_i : true
    end
  end
end