Skip to content

Commit f3e48cc

Browse files
committed
Merge branch '7.4' into 8.0
* 7.4: Minor tweaks [HttpClient] Document `CachingHttpClient` compatible with RFC 9111
2 parents de6db8f + 44d3761 commit f3e48cc

File tree

2 files changed

+187
-15
lines changed

2 files changed

+187
-15
lines changed

http_client.rst

Lines changed: 103 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,8 @@ making a request. Use the ``max_redirects`` setting to configure this behavior
619619
'max_redirects' => 0,
620620
]);
621621

622+
.. _http-client-retry-failed-requests:
623+
622624
Retry Failed Requests
623625
~~~~~~~~~~~~~~~~~~~~~
624626

@@ -1331,29 +1333,114 @@ in the foreach loop::
13311333
}
13321334
}
13331335

1336+
.. _http-client_caching:
1337+
13341338
Caching Requests and Responses
13351339
------------------------------
13361340

13371341
This component provides a :class:`Symfony\\Component\\HttpClient\\CachingHttpClient`
1338-
decorator that allows caching responses and serving them from the local storage
1339-
for next requests. The implementation leverages the
1340-
:class:`Symfony\\Component\\HttpKernel\\HttpCache\\HttpCache` class under the hood
1341-
so that the :doc:`HttpKernel component </components/http_kernel>` needs to be
1342-
installed in your application::
1342+
decorator that enables caching of HTTP responses and serving them from cache
1343+
storage on subsequent requests, as described in `RFC 9111`_.
13431344

1344-
use Symfony\Component\HttpClient\CachingHttpClient;
1345-
use Symfony\Component\HttpClient\HttpClient;
1346-
use Symfony\Component\HttpKernel\HttpCache\Store;
1345+
Internally, it relies on a :class:`tag aware cache <Symfony\\Contracts\\Cache\\TagAwareCacheInterface>`,
1346+
so the :doc:`Cache component </components/cache>` must be installed in your application.
13471347

1348-
$store = new Store('/path/to/cache/storage/');
1349-
$client = HttpClient::create();
1350-
$client = new CachingHttpClient($client, $store);
1348+
.. tip::
1349+
1350+
The caching mechanism is asynchronous. The response must be fully consumed
1351+
(for example, by calling ``getContent()`` or using a stream) for it to be
1352+
stored in the cache.
1353+
1354+
.. configuration-block::
1355+
1356+
.. code-block:: yaml
1357+
1358+
# config/packages/framework.yaml
1359+
framework:
1360+
http_client:
1361+
scoped_clients:
1362+
example.client:
1363+
base_uri: 'https://example.com'
1364+
caching:
1365+
cache_pool: example_cache_pool
1366+
1367+
cache:
1368+
pools:
1369+
example_cache_pool:
1370+
adapter: cache.adapter.redis_tag_aware
1371+
tags: true
1372+
1373+
.. code-block:: xml
1374+
1375+
<!-- config/packages/framework.xml -->
1376+
<?xml version="1.0" encoding="UTF-8" ?>
1377+
<container xmlns="http://symfony.com/schema/dic/services"
1378+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1379+
xmlns:framework="http://symfony.com/schema/dic/symfony"
1380+
xsi:schemaLocation="http://symfony.com/schema/dic/services
1381+
https://symfony.com/schema/dic/services/services-1.0.xsd
1382+
http://symfony.com/schema/dic/symfony https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
1383+
1384+
<framework:config>
1385+
<framework:http-client>
1386+
<framework:scoped-client name="example.client"
1387+
base-uri="https://example.com"
1388+
>
1389+
<framework:caching cache-pool="example_cache_pool"/>
1390+
</framework:scoped-client>
1391+
</framework:http-client>
1392+
1393+
<framework:cache>
1394+
<framework:pool name="example_cache_pool"
1395+
adapter="cache.adapter.redis_tag_aware"
1396+
tags="true"
1397+
/>
1398+
</framework:cache>
1399+
</framework:config>
1400+
</container>
1401+
1402+
.. code-block:: php
1403+
1404+
// config/packages/framework.php
1405+
use Symfony\Config\FrameworkConfig;
1406+
1407+
return static function (FrameworkConfig $framework): void {
1408+
$framework->httpClient()->scopedClient('example.client')
1409+
->baseUri('https://example.com')
1410+
->caching()
1411+
->cachePool('example_cache_pool')
1412+
// ...
1413+
;
1414+
1415+
$framework->cache()
1416+
->pool('example_cache_pool')
1417+
->adapter('cache.adapter.redis_tag_aware')
1418+
->tags(true)
1419+
;
1420+
};
1421+
1422+
.. code-block:: php-standalone
1423+
1424+
use Symfony\Component\Cache\Adapter\FilesystemTagAwareAdapter;
1425+
use Symfony\Component\HttpClient\CachingHttpClient;
1426+
use Symfony\Component\HttpClient\HttpClient;
1427+
1428+
$cache = new FilesystemTagAwareAdapter();
1429+
1430+
$client = HttpClient::createForBaseUri('https://example.com');
1431+
$cachingClient = new CachingHttpClient($client, $cache);
1432+
1433+
.. tip::
1434+
1435+
It is strongly recommended to configure a
1436+
:ref:`retry strategy <http-client-retry-failed-requests>` to gracefully
1437+
handle temporary cache inconsistencies or validation failures.
13511438

1352-
// this won't hit the network if the resource is already in the cache
1353-
$response = $client->request('GET', 'https://example.com/cacheable-resource');
1439+
.. versionadded:: 7.4
13541440

1355-
:class:`Symfony\\Component\\HttpClient\\CachingHttpClient` accepts a third argument
1356-
to set the options of the :class:`Symfony\\Component\\HttpKernel\\HttpCache\\HttpCache`.
1441+
In Symfony 7.4, caching was refactored to comply with `RFC 9111`_ and to
1442+
leverage the :doc:`Cache component </components/cache>`. In previous versions,
1443+
it relied on ``HttpCache`` from the HttpKernel component.
13571444

13581445
Limit the Number of Requests
13591446
----------------------------
@@ -2286,5 +2373,6 @@ body::
22862373
.. _`idempotent method`: https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Idempotent_methods
22872374
.. _`SSRF`: https://portswigger.net/web-security/ssrf
22882375
.. _`RFC 6570`: https://www.rfc-editor.org/rfc/rfc6570
2376+
.. _`RFC 9111`: https://www.rfc-editor.org/rfc/rfc9111
22892377
.. _`HAR`: https://w3c.github.io/web-performance/specs/HAR/Overview.html
22902378
.. _`the Cookie HTTP request header`: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cookie

reference/configuration/framework.rst

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,6 +1335,88 @@ If this option is a boolean value, the response is buffered when the value is
13351335
returned value is ``true`` (the closure receives as argument an array with the
13361336
response headers).
13371337

1338+
caching
1339+
.......
1340+
1341+
**type**: ``array``
1342+
1343+
This option configures the behavior of the :ref:`HTTP client caching <http-client_caching>`,
1344+
including which types of requests to cache and how many times. The behavior is
1345+
defined with the following options:
1346+
1347+
* :ref:`cache_pool <reference-http-client-caching-cache-pool>`
1348+
* :ref:`shared <reference-http-client-caching-shared>`
1349+
* :ref:`max_ttl <reference-http-client-caching-max-ttl>`
1350+
1351+
.. code-block:: yaml
1352+
1353+
# config/packages/framework.yaml
1354+
framework:
1355+
# ...
1356+
http_client:
1357+
# ...
1358+
default_options:
1359+
caching:
1360+
cache_pool: cache.app
1361+
shared: true
1362+
max_ttl: 86400
1363+
1364+
scoped_clients:
1365+
my_api.client:
1366+
# ...
1367+
caching:
1368+
cache_pool: my_taggable_pool
1369+
1370+
.. versionadded:: 7.4
1371+
1372+
The ``caching`` option was introduced in Symfony 7.4.
1373+
1374+
.. _reference-http-client-caching-cache-pool:
1375+
1376+
cache_pool
1377+
""""""""""
1378+
1379+
**type**: ``string``
1380+
1381+
The service ID of the cache pool used to store the cached responses. The service
1382+
must implement the :class:`Symfony\\Contracts\\Cache\\TagAwareCacheInterface`.
1383+
1384+
By default, it uses an instance of :class:`Symfony\\Component\\Cache\\Adapter\\TagAwareAdapter`
1385+
wrapping the ``cache.app`` pool.
1386+
1387+
.. versionadded:: 7.4
1388+
1389+
The ``cache_pool`` option was introduced in Symfony 7.4.
1390+
1391+
.. _reference-http-client-caching-shared:
1392+
1393+
shared
1394+
""""""
1395+
1396+
**type**: ``boolean`` **default**: ``true``
1397+
1398+
If ``true``, it uses a `shared cache`_ so cached responses can be reused across
1399+
users. Set it to ``false`` to use a `private cache`_.
1400+
1401+
.. versionadded:: 7.4
1402+
1403+
The ``shared`` option was introduced in Symfony 7.4.
1404+
1405+
.. _reference-http-client-caching-max-ttl:
1406+
1407+
max_ttl
1408+
"""""""
1409+
1410+
**type**: ``integer`` **default**: ``null``
1411+
1412+
The maximum time-to-live (in seconds) for cached responses. By default, responses
1413+
are cached for as long as the TTL specified by the server. When this option is
1414+
set, server-provided TTLs are capped to this value.
1415+
1416+
.. versionadded:: 7.4
1417+
1418+
The ``max_ttl`` option was introduced in Symfony 7.4.
1419+
13381420
cafile
13391421
......
13401422

@@ -3403,3 +3485,5 @@ to know their differences.
34033485
.. _`Link HTTP header`: https://tools.ietf.org/html/rfc5988
34043486
.. _`SMTP session`: https://en.wikipedia.org/wiki/Simple_Mail_Transfer_Protocol#SMTP_transport_example
34053487
.. _`PHP attributes`: https://www.php.net/manual/en/language.attributes.overview.php
3488+
.. _`public cache`: https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/Caching#shared_cache
3489+
.. _`private cache`: https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/Caching#private_caches

0 commit comments

Comments
 (0)