|
1 | 1 | Cache Plugin
|
2 | 2 | ============
|
3 | 3 |
|
4 |
| -TODO |
| 4 | +The ``CachePlugin`` allows you to cache responses from the server. It can use |
| 5 | +any PSR-6 compatible caching engine. By default, the plugin respects the cache |
| 6 | +control headers from the server as specified in :rfc:`7234`. It needs a |
| 7 | +:ref:`stream <stream-factory>` and a `PSR-6`_ implementation:: |
| 8 | + |
| 9 | + use Http\Discovery\HttpClientDiscovery; |
| 10 | + use Http\Client\Plugin\PluginClient; |
| 11 | + use Http\Client\Plugin\CachePlugin; |
| 12 | + |
| 13 | + /** @var \Psr\Cache\CacheItemPoolInterface $pool */ |
| 14 | + $pool... |
| 15 | + /** @var \Http\Message\StreamFactory $streamFactory */ |
| 16 | + $streamFactory... |
| 17 | + |
| 18 | + $options = []; |
| 19 | + $cachePlugin = new CachePlugin($pool, $streamFactory, $options); |
| 20 | + |
| 21 | + $pluginClient = new PluginClient( |
| 22 | + HttpClientDiscovery::find(), |
| 23 | + [$cachePlugin] |
| 24 | + ); |
| 25 | + |
| 26 | +By default, responses with no cache control headers are not cached. If you want |
| 27 | +a default cache lifetime if the server specifies no ``max-age``, use:: |
| 28 | + |
| 29 | + $options = [ |
| 30 | + 'default_ttl' => 42, // cache lifetime time in seconds |
| 31 | + ]; |
| 32 | + |
| 33 | +You can also tell the plugin to completely ignore the cache control headers |
| 34 | +from the server and force caching for the default time to life. Note that in |
| 35 | +this case, ``default_ttl`` is required:: |
| 36 | + |
| 37 | + $options = [ |
| 38 | + 'default_ttl' => 3600, // cache for one hour |
| 39 | + 'respect_cache_headers' => false, |
| 40 | + ]; |
| 41 | + |
| 42 | +Cache Control Handling |
| 43 | +---------------------- |
| 44 | + |
| 45 | +This plugin does not cache responses with ``no-store`` or ``private`` instructions. |
| 46 | + |
| 47 | +It does store responses with cookies or a ``Set-Cookie`` header. Be careful with |
| 48 | +the order of your plugins. |
| 49 | + |
| 50 | +.. _PSR-6: http://www.php-fig.org/psr/psr-6/ |
0 commit comments