2017 © Pedro Peláez
 

library psr7-cache

Cache and conditional request helpers for PSR-7 HTTP Messages

image

micheh/psr7-cache

Cache and conditional request helpers for PSR-7 HTTP Messages

  • Friday, April 15, 2016
  • by micheh
  • Repository
  • 6 Watchers
  • 36 Stars
  • 26,356 Installations
  • PHP
  • 11 Dependents
  • 2 Suggesters
  • 3 Forks
  • 0 Open issues
  • 6 Versions
  • 11 % Grown

The README.md

Cache helpers for PSR-7 HTTP Messages

Build Status Code Coverage Scrutinizer Code Quality, (*1)

This library provides an easy way to either add cache relevant headers to a PSR-7 HTTP message implementation, or to extract cache and conditional request information from a PSR-7 message (e.g. if a response is cacheable). It also provides a Cache-Control value object to provide an object oriented interface for the manipulation of the Cache-Control header., (*2)

Installation

Install this library using Composer:, (*3)

$ composer require micheh/psr7-cache

Quickstart

To enable caching of HTTP responses, create an instance of CacheUtil, call the method withCache and provide your PSR-7 response., (*4)

/** @var \Psr\Http\Message\ResponseInterface $response */

$util = new \Micheh\Cache\CacheUtil();
$response = $util->withCache($response);

This will add the header Cache-Control: private, max-age=600 to your response. With this header the response will only be cached by the client who sent the request and will be cached for 600 seconds (10 min). During this time the client should use the response from the cache and should not make a new request to the application., (*5)

Cache Validators

The application should also add Cache Validators to the response: An ETag header (and Last-Modified header if you know when the resource was last modified). This way the client will also include the ETag and Last-Modified information in the request and the application can check if the client still has the current state., (*6)

/** @var \Psr\Http\Message\ResponseInterface $response */

$util = new \Micheh\Cache\CacheUtil();
$response = $util->withCache($response);
$response = $util->withETag($response, 'my-etag');
$response = $util->withLastModified($response, new \DateTime());

Revalidate a response

To determine if the client still has a current copy of the page and the response is not modified, you can use the isNotModified method. Simply add the cache headers to the response and then call the method with both the request and the response. The method will automatically compare the If-None-Match header of the request with the ETag header of the response (and/or the If-Modified-Since header of the request with the Last-Modified header of the response if available). If the response is not modified, return the empty response with the cache headers and a status code 304 (Not Modified). This will instruct the client to use the cached copy from the previous request, saving you CPU/memory usage and bandwidth. Therefore it is important to keep the code before the isNotModified call as lightweight as possible to increase performance. Don't create the complete response before this method., (*7)

/** @var \Psr\Http\Message\RequestInterface $request */
/** @var \Psr\Http\Message\ResponseInterface $response */

$util = new \Micheh\Cache\CacheUtil();
$response = $util->withCache($response);
$response = $util->withETag($response, 'my-etag');
$response = $util->withLastModified($response, new \DateTime());

if ($util->isNotModified($request, $response)) {
    return $response->withStatus(304);
}

// create the body of the response

Conditional request with unsafe method

While the procedure described above is usually optional and for safe methods (GET and HEAD), it is also possible to enforce that the client has the current resource state. This is useful for unsafe methods (e.g. POST, PUT, PATCH or DELETE), because it can prevent lost updates (e.g. if another client updates the resource before your request). It is a good idea to initially check if the request includes the appropriate headers (If-Match for an ETag and/or If-Unmodified-Since for a Last-Modified date) with the hasStateValidator method. If the request does not include this information, abort the execution and return status code 428 (Precondition Required) or status code 403 (Forbidden) if you only want to use the original status codes., (*8)

/** @var \Psr\Http\Message\RequestInterface $request */

$util = new \Micheh\Cache\CacheUtil();
if (!$util->hasStateValidator($request)) {
    return $response->withStatus(428);
}

If the state validators are included in the request, you can check if the request has the current resource state and not an outdated version with the method hasCurrentState. If the request has an outdated resource state (another ETag or an older Last-Modified date), abort the execution and return status code 412 (Precondition Failed). Otherwise you can continue to process the request and update/delete the resource. Once the resource is updated, it is a good idea to include the updated ETag (and Last-Modified date if available) in the response., (*9)

/** @var \Psr\Http\Message\RequestInterface $request */
/** @var \Psr\Http\Message\ResponseInterface $response */

$util = new \Micheh\Cache\CacheUtil();
if (!$util->hasStateValidator($request)) {
    return $response->withStatus(428);
}

$eTag = 'my-etag'
$lastModified = new \DateTime();
if (!$util->hasCurrentState($request, $eTag, $lastModified)) {
    return $response->withStatus(412);
}

// process the request

Available helper methods

Method Description (see the phpDoc for more information)
withCache Convenience method to add a Cache-Control header, which allows caching
withCachePrevention Convenience method to prevent caching
withExpires Adds an Expires header from a timestamp, string or DateTime
withRelativeExpires Adds an Expires header with a specific lifetime
withETag Adds an ETag header
withLastModified Adds a Last-Modified header from a timestamp, string or DateTime
withCacheControl Adds a Cache-Control header from a string or value object
hasStateValidator Checks if it is possible to determine the resource state
hasCurrentState Checks if a request has the current resource state
isNotModified Checks if a response is not modified
isCacheable Checks if a response is cacheable by a public cache
isFresh Checks if a response is fresh (age smaller than lifetime)
getLifetime Returns the lifetime of a response (how long it should be cached)
getAge Returns the age of a response (how long it was in the cache)

References

License

The files in this archive are licensed under the BSD-3-Clause license. You can find a copy of this license in LICENSE.md., (*10)

The Versions

15/04 2016

dev-master

9999999-dev

Cache and conditional request helpers for PSR-7 HTTP Messages

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

psr psr-7 cache http request response http-message conditional if-match if-none-match if-modified-since if-unmodified-since

15/04 2016

0.5

0.5.0.0

Cache and conditional request helpers for PSR-7 HTTP Messages

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

psr psr-7 cache http request response http-message conditional if-match if-none-match if-modified-since if-unmodified-since

17/02 2016

0.4

0.4.0.0

Cache helpers for PSR-7 HTTP Messages

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

psr psr-7 cache http request response http-message

20/08 2015

0.3

0.3.0.0

Cache helpers for PSR-7 HTTP Messages

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

psr psr-7 cache http request response http-message

17/08 2015

0.2

0.2.0.0

Cache helpers for PSR-7 HTTP Messages

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

psr psr-7 cache http request response http-message

15/08 2015

0.1

0.1.0.0

Cache helpers for PSR-7 HTTP Messages

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

psr psr-7 cache http request response http-message