HTTPCacheControlMiddleware
class HTTPCacheControlMiddleware implements HTTPMiddleware, Resettable (View source)
Traits
Provides extensions to this object to integrate it with standard config API methods.
A class that can be instantiated or replaced via DI
Constants
STATE_ENABLED |
|
STATE_PUBLIC |
|
STATE_PRIVATE |
|
STATE_DISABLED |
|
LEVEL_FORCED |
Forcing level forced, optionally combined with one of the below. |
LEVEL_DISABLED |
Forcing level caching disabled. Overrides public/private. |
LEVEL_PRIVATE |
Forcing level private-cached. Overrides public. |
LEVEL_PUBLIC |
Forcing level public cached. Lowest priority. |
LEVEL_ENABLED |
Forcing level caching enabled. |
Config options
defaultState | string | Set default state |
|
defaultVary | array | Default vary |
|
defaultForcingLevel | int | Default forcing level |
|
allowed_directives | array | A list of allowed cache directives for HTTPResponses |
Properties
protected | array | $stateDirectives | List of states, each of which contains a key of standard directives. |
|
protected | string | $state | Current state |
|
protected | int | $forcingLevel | Forcing level of previous setting; higher number wins Combination of consts below |
|
protected | array|null | $vary | List of vary keys |
|
protected | int | $modificationDate | Latest modification date for this response |
Methods
Get a configuration accessor for this class. Short hand for Config::inst()->get($this->class, .....).
Gets the uninherited value for the given config option
An implementation of the factory method, allows you to create an instance of a class
Creates a class instance by the "singleton" design pattern.
Combine vary strings/arrays into a single array, or normalise a single vary
Register a modification date. Used to calculate the "Last-Modified" HTTP header.
Set current state. Should only be invoked internally after processing precedence rules.
Instruct the cache to apply a change with a given level, optionally modifying it with a force flag to increase priority of this action.
Low level method for setting directives include any experimental or custom ones added via config.
Low level method to set directives from an associative array
Low level method for removing directives
Low level method to check if a directive is currently set
Low level method to get the value of a directive for a state.
Get the value of the given directive for the current state
The cache should not store anything about the client request or server response.
Forces caches to submit the request to the origin server for validation before releasing a cached copy.
Specifies the maximum amount of time (seconds) a resource will be considered fresh.
Overrides max-age or the Expires header, but it only applies to shared caches (e.g., proxies) and is ignored by a private cache.
The cache must verify the status of the stale resources before using it and expired ones should not be used.
Simple way to set cache control header to a cacheable state.
Simple way to set cache control header to a non-cacheable state.
Advanced way to set cache control header to a non-cacheable state.
Advanced way to set cache control header to a cacheable state.
Update state based on current request and response objects
Details
static Config_ForClass
config()
Get a configuration accessor for this class. Short hand for Config::inst()->get($this->class, .....).
mixed
uninherited(string $name)
Gets the uninherited value for the given config option
static Injectable
create(mixed ...$args)
An implementation of the factory method, allows you to create an instance of a class
This method will defer class substitution to the Injector API, which can be customised via the Config API to declare substitution classes.
This can be called in one of two ways - either calling via the class directly, or calling on Object and passing the class name as the first parameter. The following are equivalent: $list = DataList::create(SiteTree::class); $list = SiteTree::get();
static Injectable
singleton(string $class = null)
Creates a class instance by the "singleton" design pattern.
It will always return the same instance for this class, which can be used for performance reasons and as a simple way to access instance methods which don't rely on instance data (e.g. the custom SilverStripe static handling).
HTTPResponse
process(HTTPRequest $request, callable $delegate)
Generate response for the given request
array
getVary()
Get current vary keys
$this
addVary(string|array $vary)
Add a vary
$this
setVary(array|string $vary)
Set vary
protected array
combineVary(string|array[] ...$varies)
Combine vary strings/arrays into a single array, or normalise a single vary
HTTPCacheControlMiddleware
registerModificationDate(string|int $date)
Register a modification date. Used to calculate the "Last-Modified" HTTP header.
Can be called multiple times, and will automatically retain the most recent date.
protected $this
setState(string $state)
Set current state. Should only be invoked internally after processing precedence rules.
string
getState()
Get current state
protected bool
applyChangeLevel(int $level, bool $force)
Instruct the cache to apply a change with a given level, optionally modifying it with a force flag to increase priority of this action.
If the apply level was successful, the change is made and the internal level threshold is incremented.
$this
setStateDirective(array|string $states, string $directive, int|string|bool $value = true)
Low level method for setting directives include any experimental or custom ones added via config.
You need to specify the state (or states) to apply this directive to. Can also remove directives with false
$this
setStateDirectivesFromArray(array|string $states, array $directives)
Low level method to set directives from an associative array
$this
removeStateDirective(array|string $states, string $directive)
Low level method for removing directives
bool
hasStateDirective(string $state, string $directive)
Low level method to check if a directive is currently set
bool
hasDirective(string $directive)
Check if the current state has the given directive.
int|string|bool
getStateDirective(string $state, string $directive)
Low level method to get the value of a directive for a state.
Returns false if there is no directive. True means the flag is set, otherwise the value of the directive.
bool|int|string
getDirective(string $directive)
Get the value of the given directive for the current state
array
getStateDirectives(string $state)
Get directives for the given state
array
getDirectives()
Get all directives for the currently active state
$this
setNoStore(bool $noStore = true)
The cache should not store anything about the client request or server response.
Affects all non-disabled states. Use setStateDirective() instead to set for a single state. Set the no-store directive (also removes max-age and s-maxage for consistency purposes)
$this
setNoCache(bool $noCache = true)
Forces caches to submit the request to the origin server for validation before releasing a cached copy.
Affects all non-disabled states. Use setStateDirective() instead to set for a single state.
$this
setMaxAge(int $age)
Specifies the maximum amount of time (seconds) a resource will be considered fresh.
This directive is relative to the time of the request. Affects all non-disabled states. Use enableCache(), publicCache() or setStateDirective() instead to set the max age for a single state.
$this
setSharedMaxAge(int $age)
Overrides max-age or the Expires header, but it only applies to shared caches (e.g., proxies) and is ignored by a private cache.
Affects all non-disabled states. Use setStateDirective() instead to set for a single state.
$this
setMustRevalidate(bool $mustRevalidate = true)
The cache must verify the status of the stale resources before using it and expired ones should not be used.
Affects all non-disabled states. Use setStateDirective() instead to set for a single state.
$this
enableCache(bool $force = false, int $maxAge = null)
Simple way to set cache control header to a cacheable state.
Needs either setMaxAge()
or the $maxAge
method argument in order to activate caching.
The resulting cache-control headers will be chosen from the 'enabled' set of directives.
Does not set public
directive. Usually, setMaxAge()
is sufficient. Use publicCache()
if this is explicitly required.
See https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/http-caching#public_vs_private
$this
disableCache(bool $force = false)
Simple way to set cache control header to a non-cacheable state.
Use this method over privateCache()
if you are unsure about caching details.
Takes precedence over unforced enableCache()
, privateCache()
or publicCache()
calls.
The resulting cache-control headers will be chosen from the 'disabled' set of directives.
Removes all state and replaces it with no-cache, no-store, must-revalidate
. Although no-store
is sufficient
the others are added under recommendation from Mozilla (https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#Examples)
Does not set private
directive, use privateCache()
if this is explicitly required.
See https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/http-caching#public_vs_private
$this
privateCache(bool $force = false)
Advanced way to set cache control header to a non-cacheable state.
Indicates that the response is intended for a single user and must not be stored by a shared cache. A private cache (e.g. Web Browser) may store the response.
The resulting cache-control headers will be chosen from the 'private' set of directives.
$this
publicCache(bool $force = false, int $maxAge = null)
Advanced way to set cache control header to a cacheable state.
Indicates that the response may be cached by any cache. (eg: CDNs, Proxies, Web browsers).
Needs either setMaxAge()
or the $maxAge
method argument in order to activate caching.
The resulting cache-control headers will be chosen from the 'private' set of directives.
$this
applyToResponse(HTTPResponse $response)
Generate all headers to add to this object
protected string
generateCacheHeader()
Generate the cache header
array
generateHeadersFor(HTTPResponse $response)
Generate all headers to output
static
reset()
Reset registered http cache control and force a fresh instance to be built
protected int
getForcingLevel()
No description
protected string|null
generateVaryHeader(HTTPResponse $response)
Generate vary http header
protected string|null
generateLastModifiedHeader()
Generate Last-Modified header
protected null|string
generateExpiresHeader()
Generate Expires http header
protected
augmentState(HTTPRequest $request, HTTPResponse $response)
Update state based on current request and response objects