Skip to content

document cache plugin #86

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 19, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 47 additions & 1 deletion plugins/cache.rst
Original file line number Diff line number Diff line change
@@ -1,4 +1,50 @@
Cache Plugin
============

TODO
The ``CachePlugin`` allows you to cache responses from the server. It can use
any PSR-6 compatible caching engine. By default, the plugin respects the cache
control headers from the server as specified in :rfc:`7234`. It needs a
:ref:`stream <stream-factory>` and a `PSR-6`_ implementation::

use Http\Discovery\HttpClientDiscovery;
use Http\Client\Plugin\PluginClient;
use Http\Client\Plugin\CachePlugin;

/** @var \Psr\Cache\CacheItemPoolInterface $pool */
$pool...
/** @var \Http\Message\StreamFactory $streamFactory */
$streamFactory...

$options = [];
$cachePlugin = new CachePlugin($pool, $streamFactory, $options);

$pluginClient = new PluginClient(
HttpClientDiscovery::find(),
[$cachePlugin]
);

By default, responses with no cache control headers are not cached. If you want
a default cache lifetime if the server specifies no ``max-age``, use::

$options = [
'default_ttl' => 42, // cache lifetime time in seconds
];

You can also tell the plugin to completely ignore the cache control headers
from the server and force caching for the default time to life. Note that in
this case, ``default_ttl`` is required::

$options = [
'default_ttl' => 3600, // cache for one hour
'respect_cache_headers' => false,
];

Cache Control Handling
----------------------

This plugin does not cache responses with ``no-store`` or ``private`` instructions.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

feel like we should also respect no-cache. php-http/plugins#58


It does store responses with cookies or a ``Set-Cookie`` header. Be careful with
the order of your plugins.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Nyholm i created a bunch of issues on the plugin repo with things i noticed (some are simply new features, but some could be problematic). i did not create one for the cookie topic. i am not sure about this one. on a proxy, you never cache if there are Authorization or Cookie headers around, for fear of mixing up content. for a client this is less clear, depends on the scenario. might require another option on the plugin?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added to php-http/plugins#58 , i think splitting that plugin into two for the different roles makes more and more sense.


.. _PSR-6: http://www.php-fig.org/psr/psr-6/