Skip to content
Add HTTP caching to the response
PHP
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
config
src
.editorconfig
.gitattributes
.gitignore
.scrutinizer.yml
.styleci.yml
.travis.yml
CHANGELOG.md
CODE_OF_CONDUCT.md
CONTRIBUTING.md
ISSUE_TEMPLATE.md
LICENSE.md
PULL_REQUEST_TEMPLATE.md
README.md
composer.json
initialize.php
phpcs.xml.dist
phpunit.xml.dist

README.md

Cache Control

Add HTTP caching to the response

Install

Via Composer

$ composer require getpop/cache-control dev-master

Note: Your composer.json file must have the configuration below to accept minimum stability "dev" (there are no releases for PoP yet, and the code is installed directly from the master branch):

{
    ...
    "minimum-stability": "dev",
    "prefer-stable": true,
    ...
}

How it works

It adds a mandatory directive <cacheControl> to all fields, which has a max-age value set for each field.

The response will send a Cache-Control header with the lowest max-age from all the requested fields, or no-store if any field has max-age: 0.

Examples

Note:
Click on the following links below, and inspect the response headers using Chrome or Firefox's developer tools' Network tab.

Operators have a max-age of 1 year:

/?query=
  echo(Hello world!)

[View query results]

By default, fields have a max-age of 1 hour:

/?query=
  echo(Hello world!)|
  posts.
    title

[View query results]

Nested fields are also taken into account when computing the lowest max-age:

/?query=
  echo(posts())

[View query results]

"time" field is not to be cached (max-age: 0):

/?query=
  time

[View query results]

Ways to not cache a response:

a. Add field "time" to the query:

/?query=
  time|
  echo(Hello world!)|
  posts.
    title

[View query results]

b. Override the default maxAge configuration for a field, by adding argument maxAge: 0 to directive <cacheControl>:

/?query=
  echo(Hello world!)|
  posts.
    title<cacheControl(maxAge:0)>

[View query results]

Change log

Please see CHANGELOG for more information on what has changed recently.

Testing

$ composer test

Contributing

Please see CONTRIBUTING and CODE_OF_CONDUCT for details.

Security

If you discover any security related issues, please email leo@getpop.org instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

You can’t perform that action at this time.