Documentation

CachePlugin
in package
implements EventSubscriberInterface

Plugin to enable the caching of GET and HEAD requests. Caching can be done on all requests passing through this plugin or only after retrieving resources with cacheable response headers.

This is a simple implementation of RFC 2616 and should be considered a private transparent proxy cache, meaning authorization and private data can be cached.

It also implements RFC 5861's stale-if-error Cache-Control extension, allowing stale cache responses to be used when an error is encountered (such as a 500 Internal Server Error or DNS failure).

Interfaces, Classes and Traits

EventSubscriberInterface
An EventSubscriber knows itself what events it is interested in.

Table of Contents

$autoPurge  : bool
$canCache  : CanCacheStrategyInterface
$revalidation  : RevalidationInterface
$storage  : CacheStorageInterface
__construct()  : mixed
canResponseSatisfyFailedRequest()  : bool
Check if a cache response satisfies a failed request's caching constraints
canResponseSatisfyRequest()  : bool
Check if a cache response satisfies a request's caching constraints
getSubscribedEvents()  : array<string|int, mixed>
Returns an array of event names this subscriber wants to listen to.
onRequestBeforeSend()  : mixed
Check if a response in cache will satisfy the request before sending
onRequestError()  : mixed
If possible, return a cache response on an error
onRequestException()  : null
If possible, set a cache response on a cURL exception
onRequestSent()  : mixed
If possible, store a response in cache after sending
purge()  : mixed
Purge all cache entries for a given URL
addResponseHeaders()  : mixed
Add the plugin's headers to a response

Properties

Methods

__construct()

public __construct([array<string|int, mixed>|CacheAdapterInterface|CacheStorageInterface $options = null ]) : mixed
Parameters
$options : array<string|int, mixed>|CacheAdapterInterface|CacheStorageInterface = null

Array of options for the cache plugin, cache adapter, or cache storage object.

  • CacheStorageInterface storage: Adapter used to cache responses
  • RevalidationInterface revalidation: Cache revalidation strategy
  • CanCacheInterface can_cache: Object used to determine if a request can be cached
  • bool auto_purge Set to true to automatically PURGE resources when non-idempotent requests are sent to a resource. Defaults to false.
Tags
throws
InvalidArgumentException

if no cache is provided and Doctrine cache is not installed

Return values
mixed

canResponseSatisfyFailedRequest()

Check if a cache response satisfies a failed request's caching constraints

public canResponseSatisfyFailedRequest(RequestInterface $request, Response $response) : bool
Parameters
$request : RequestInterface

Request to validate

$response : Response

Response to validate

Return values
bool

canResponseSatisfyRequest()

Check if a cache response satisfies a request's caching constraints

public canResponseSatisfyRequest(RequestInterface $request, Response $response) : bool
Parameters
$request : RequestInterface

Request to validate

$response : Response

Response to validate

Return values
bool

getSubscribedEvents()

Returns an array of event names this subscriber wants to listen to.

public static getSubscribedEvents() : array<string|int, mixed>

The array keys are event names and the value can be:

  • The method name to call (priority defaults to 0)
  • An array composed of the method name to call and the priority
  • An array of arrays composed of the method names to call and respective priorities, or 0 if unset

For instance:

  • ['eventName' => 'methodName']
  • ['eventName' => ['methodName', $priority]]
  • ['eventName' => [['methodName1', $priority], ['methodName2']]]

The code must not depend on runtime state as it will only be called at compile time. All logic depending on runtime state must be put into the individual methods handling the events.

Return values
array<string|int, mixed>

The event names to listen to

onRequestBeforeSend()

Check if a response in cache will satisfy the request before sending

public onRequestBeforeSend(Event $event) : mixed
Parameters
$event : Event
Return values
mixed

onRequestError()

If possible, return a cache response on an error

public onRequestError(Event $event) : mixed
Parameters
$event : Event
Return values
mixed

onRequestException()

If possible, set a cache response on a cURL exception

public onRequestException(Event $event) : null
Parameters
$event : Event
Return values
null

onRequestSent()

If possible, store a response in cache after sending

public onRequestSent(Event $event) : mixed
Parameters
$event : Event
Return values
mixed

purge()

Purge all cache entries for a given URL

public purge(string $url) : mixed
Parameters
$url : string

URL to purge

Return values
mixed

Search results