@@ -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+
622624Retry Failed Requests
623625~~~~~~~~~~~~~~~~~~~~~
624626
@@ -1331,29 +1333,114 @@ in the foreach loop::
13311333 }
13321334 }
13331335
1336+ .. _http-client_caching :
1337+
13341338Caching Requests and Responses
13351339------------------------------
13361340
13371341This 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
13581445Limit 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
0 commit comments