-
Notifications
You must be signed in to change notification settings - Fork 56
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
Changes from 1 commit
2ceccfc
ddf6591
9434000
7f0f1bf
d8d0fef
540c826
58a727c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
|
@@ -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 recieved 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 | ||
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:: | ||
If you tell the plugin to completely ignore the cache control headers from the server and force caching the response | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why "If you tell"? could be "You can tell" |
||
for the default time to life. The options below will cache all responses for one hour:: | ||
|
||
$options = [ | ||
'default_ttl' => 3600, // cache for one hour | ||
'respect_cache_headers' => false, | ||
]; | ||
|
||
Be ware of null values | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are correct. I guess the 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 30 days it will be | ||
removed from the cache:: | ||
|
||
$options = [ | ||
'default_ttl' => 3600, | ||
'respect_cache_headers' => false, | ||
'cache_lifetime' => 86400*30, // 30 days | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i would take something else, as 30 is the default. |
||
]; | ||
|
||
|
||
|
||
Cache Control Handling | ||
---------------------- | ||
|
||
|
There was a problem hiding this comment.
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.