Skip to content

Added more options examples #144

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 7 commits into from
Aug 15, 2016
Merged
Show file tree
Hide file tree
Changes from 3 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
70 changes: 63 additions & 7 deletions plugins/cache.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ control headers from the server as specified in :rfc:`7234`. It needs a
use Http\Client\Common\Plugin\CachePlugin;

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

$options = [];
$cachePlugin = new CachePlugin($pool, $streamFactory, $options);
Expand All @@ -33,22 +33,78 @@ control headers from the server as specified in :rfc:`7234`. It needs a
[$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
-------

The third parameter to the ``CachePlugin`` constructor takes an array of options. The plugin has 3 options you can
configure. Their default values and meaning is described by the table below.

+---------------------------+---------------+------------------------------------------------------+
| Name | Default value | Description |
+===========================+===============+======================================================+
| ``default_ttl`` | ``null`` | The default max age of a Response |
+---------------------------+---------------+------------------------------------------------------+
| ``respect_cache_headers`` | ``true`` | Whatever or not we should care about cache headers |
+---------------------------+---------------+------------------------------------------------------+
| ``cache_lifetime`` | 30 days | The minimum time we should store a cache item |
+---------------------------+---------------+------------------------------------------------------+

.. note::

A HTTP response may have expired but it is still in cache. If so, headers like ``If-Modified-Since`` and
``If-None-Match`` are added to the HTTP request to allow the server answer with 304 status code. When
a 304 response is received we update the CacheItem and save it again for at least ``cache_lifetime``.

Using these options together you may control the how your cached responses behave. By default, responses with no
Copy link
Contributor

Choose a reason for hiding this comment

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

"you may control the how your cached responses behave" => you can control how your responses should be cached.

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::
You can tell the plugin to completely ignore the cache control headers from the server and force caching the response
for the default time to live. The options below will cache all responses for one hour::

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

Semantics of null values
````````````````````````

Setting null to the options ``cache_lifetime`` or ``default_ttl`` means "Store this as long as you can (forever)".
This could be a great thing when you requesting a pay-per-request API (eg. GoogleTranslate).
Copy link
Contributor

Choose a reason for hiding this comment

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

this maxe me think we should have the default_ttl at 10 minutes or so. is it currently that if there are no max-age instructions we cache the request forever? or only in combination with respect_cache_headers?

Copy link
Member Author

Choose a reason for hiding this comment

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

You are correct. I guess the default_ttl=null has changed it's behavior since the initial release.

But if there is no headers that enforce caching we should not cache the response for 10 minutes.. We should default the ttl to 0. (No cache).


Store a response as long the cache implementation allows::

$options = [
'default_ttl' => null,
'respect_cache_headers' => false,
'cache_lifetime' => null,
];


Ask the server if the response is valid at most ever hour. Store the cache item forever::

$options = [
'default_ttl' => 3600,
'respect_cache_headers' => false,
'cache_lifetime' => null,
];


Ask the server if the response is valid at most ever hour. If the response has not been used within one year it will be
removed from the cache::

$options = [
'default_ttl' => 3600,
'respect_cache_headers' => false,
'cache_lifetime' => 86400*365, // one year
];



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

Expand Down
1 change: 1 addition & 0 deletions spelling_word_list.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ callables
cURL
Diactoros
Elasticsearch
eg
Copy link
Contributor

Choose a reason for hiding this comment

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

isn't the correct spelling of this "e.g." with dots?

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah two dots, maybe. I'll test if it works. Just writing "eg." did not work.

fallback
GitHub
hotfix
Expand Down