class CacheControl
Represents the cache-control
header, which is a list of cache directives.
Definitions
PRIVATE = "private"
The private
directive indicates that the response is intended for a single user and must not be stored by shared caches.
PUBLIC = "public"
The public
directive indicates that the response may be stored by any cache, even if it would normally be considered non-cacheable.
NO_CACHE = "no-cache"
The no-cache
directive indicates that caches must revalidate the response with the origin server before serving it to clients.
NO_STORE = "no-store"
The no-store
directive indicates that caches must not store the response under any circumstances.
MAX_AGE = "max-age"
The max-age
directive indicates the maximum amount of time, in seconds, that a response is considered fresh.
S_MAXAGE = "s-maxage"
The s-maxage
directive is similar to max-age
but applies only to shared caches. If both s-maxage
and max-age
are present, s-maxage
takes precedence in shared caches.
STATIC = "static"
The static
directive is a custom directive often used to indicate that the resource is immutable or rarely changes, allowing longer caching periods.
DYNAMIC = "dynamic"
The dynamic
directive is a custom directive used to indicate that the resource is generated dynamically and may change frequently, requiring shorter caching periods.
STREAMING = "streaming"
The streaming
directive is a custom directive used to indicate that the resource is intended for progressive or chunked delivery, such as live video streams.
MUST_REVALIDATE = "must-revalidate"
The must-revalidate
directive indicates that once a response becomes stale, caches must not use it to satisfy subsequent requests without revalidating it with the origin server.
PROXY_REVALIDATE = "proxy-revalidate"
The proxy-revalidate
directive is similar to must-revalidate
but applies only to shared caches.
def initialize(value = nil)
Initializes the cache control header with the given value. The value is expected to be a comma-separated string of cache directives.
Signature
-
parameter
value
String | Nil
the raw Cache-Control header value.
Implementation
def initialize(value = nil)
super(value&.downcase)
end
def << value
Adds a directive to the Cache-Control header. The value will be normalized to lowercase before being added.
Signature
-
parameter
value
String
the directive to add.
Implementation
def << value
super(value.downcase)
end
def static?
Signature
-
returns
Boolean
whether the
static
directive is present.
Implementation
def static?
self.include?(STATIC)
end
def dynamic?
Signature
-
returns
Boolean
whether the
dynamic
directive is present.
Implementation
def dynamic?
self.include?(DYNAMIC)
end
def streaming?
Signature
-
returns
Boolean
whether the
streaming
directive is present.
Implementation
def streaming?
self.include?(STREAMING)
end
def private?
Signature
-
returns
Boolean
whether the
private
directive is present.
Implementation
def private?
self.include?(PRIVATE)
end
def public?
Signature
-
returns
Boolean
whether the
public
directive is present.
Implementation
def public?
self.include?(PUBLIC)
end
def no_cache?
Signature
-
returns
Boolean
whether the
no-cache
directive is present.
Implementation
def no_cache?
self.include?(NO_CACHE)
end
def no_store?
Signature
-
returns
Boolean
whether the
no-store
directive is present.
Implementation
def no_store?
self.include?(NO_STORE)
end
def must_revalidate?
Signature
-
returns
Boolean
whether the
must-revalidate
directive is present.
Implementation
def must_revalidate?
self.include?(MUST_REVALIDATE)
end
def proxy_revalidate?
Signature
-
returns
Boolean
whether the
proxy-revalidate
directive is present.
Implementation
def proxy_revalidate?
self.include?(PROXY_REVALIDATE)
end
def max_age
Signature
-
returns
Integer | Nil
the value of the
max-age
directive in seconds, ornil
if the directive is not present or invalid.
Implementation
def max_age
find_integer_value(MAX_AGE)
end
def s_maxage
Signature
-
returns
Integer | Nil
the value of the
s-maxage
directive in seconds, ornil
if the directive is not present or invalid.
Implementation
def s_maxage
find_integer_value(S_MAXAGE)
end
def find_integer_value(value_name)
Finds and parses an integer value from a directive.
Signature
-
parameter
value_name
String
the directive name to search for (e.g., "max-age").
-
returns
Integer | Nil
the parsed integer value, or
nil
if not found or invalid.
Implementation
def find_integer_value(value_name)
if value = self.find { |value| value.start_with?(value_name) }
_, age = value.split("=", 2)
if age =~ /\A[0-9]+\z/
return Integer(age)
end
end
end