@@ -740,6 +740,8 @@ making a request. Use the ``max_redirects`` setting to configure this behavior
740740 'max_redirects' => 0,
741741 ]);
742742
743+ .. _http-client-retry-failed-requests :
744+
743745Retry Failed Requests
744746~~~~~~~~~~~~~~~~~~~~~
745747
@@ -1490,25 +1492,106 @@ Caching Requests and Responses
14901492------------------------------
14911493
14921494This component provides a :class: `Symfony\\ Component\\ HttpClient\\ CachingHttpClient `
1493- decorator that allows caching responses and serving them from the local storage
1494- for next requests. The implementation leverages the
1495- :class: `Symfony\\ Component\\ HttpKernel\\ HttpCache\\ HttpCache ` class under the hood
1496- so that the :doc: `HttpKernel component </components/http_kernel >` needs to be
1495+ decorator that allows caching responses and serving them from the cache storage
1496+ for next requests as described in the `RFC 9111 `_.
1497+
1498+ The implementation leverages the
1499+ :class: `Symfony\\ Contracts\\ Cache\\ TagAwareCacheInterface ` class under the hood
1500+ so the :doc: `Cache component </components/cache >` needs to be
14971501installed in your application::
14981502
1499- use Symfony\Component\HttpClient\CachingHttpClient;
1500- use Symfony\Component\HttpClient\HttpClient;
1501- use Symfony\Component\HttpKernel\HttpCache\Store;
1503+ .. tip ::
1504+ The implementation is asynchronous, so the response must be consumed
1505+ (e.g., via getContent() or streaming) for caching to occur.
15021506
1503- $store = new Store('/path/to/cache/storage/');
1504- $client = HttpClient::create();
1505- $client = new CachingHttpClient($client, $store);
1507+ .. configuration-block ::
1508+
1509+ .. code-block :: yaml
1510+
1511+ # config/packages/framework.yaml
1512+ framework :
1513+ http_client :
1514+ scoped_clients :
1515+ example.client :
1516+ base_uri : ' https://example.com'
1517+ caching :
1518+ cache_pool : example_cache_pool
1519+
1520+ cache :
1521+ pools :
1522+ example_cache_pool :
1523+ adapter : cache.adapter.redis_tag_aware
1524+ tags : true
1525+
1526+ .. code-block :: xml
1527+
1528+ <!-- config/packages/framework.xml -->
1529+ <?xml version =" 1.0" encoding =" UTF-8" ?>
1530+ <container xmlns =" http://symfony.com/schema/dic/services"
1531+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
1532+ xmlns : framework =" http://symfony.com/schema/dic/symfony"
1533+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
1534+ https://symfony.com/schema/dic/services/services-1.0.xsd
1535+ http://symfony.com/schema/dic/symfony https://symfony.com/schema/dic/symfony/symfony-1.0.xsd" >
1536+
1537+ <framework : config >
1538+ <framework : http-client >
1539+ <framework : scoped-client name =" example.client"
1540+ base-uri =" https://example.com"
1541+ >
1542+ <framework : caching cache-pool =" example_cache_pool" />
1543+ </framework : scoped-client >
1544+ </framework : http-client >
1545+
1546+ <framework : cache >
1547+ <framework : pool name =" example_cache_pool"
1548+ adapter =" cache.adapter.redis_tag_aware"
1549+ tags =" true"
1550+ />
1551+ </framework : cache >
1552+ </framework : config >
1553+ </container >
1554+
1555+ .. code-block :: php
1556+
1557+ // config/packages/framework.php
1558+ use Symfony\Config\FrameworkConfig;
1559+
1560+ return static function (FrameworkConfig $framework): void {
1561+ $framework->httpClient()->scopedClient('example.client')
1562+ ->baseUri('https://example.com')
1563+ ->caching()
1564+ ->cachePool('example_cache_pool')
1565+ // ...
1566+ ;
1567+
1568+ $framework->cache()
1569+ ->pool('example_cache_pool')
1570+ ->adapter('cache.adapter.redis_tag_aware')
1571+ ->tags(true)
1572+ ;
1573+ };
1574+
1575+ .. code-block :: php-standalone
1576+
1577+ use Symfony\Component\Cache\Adapter\FilesystemTagAwareAdapter;
1578+ use Symfony\Component\HttpClient\HttpClient;
1579+ use Symfony\Component\HttpClient\CachingHttpClient;
1580+
1581+ $cache = new FilesystemTagAwareAdapter();
1582+
1583+ $client = HttpClient::createForBaseUri('https://example.com');
1584+ $cachingClient = new CachingHttpClient($client, $cache);
1585+
1586+ .. tip ::
1587+ It is also highly recommended to configure a :ref: `retry strategy <http-client-retry-failed-requests >`
1588+ to gracefully handle cache inconsistency.
15061589
1507- // this won't hit the network if the resource is already in the cache
1508- $response = $client->request('GET', 'https://example.com/cacheable-resource');
1590+ .. versionadded :: 7.4
15091591
1510- :class: `Symfony\\ Component\\ HttpClient\\ CachingHttpClient ` accepts a third argument
1511- to set the options of the :class: `Symfony\\ Component\\ HttpKernel\\ HttpCache\\ HttpCache `.
1592+ Compliance with `RFC 9111 `_ and leveraging the
1593+ :doc: `Cache component </components/cache >` was introduced in Symfony 7.4.
1594+ Prior to this, it used ``HttpCache `` from the HttpKernel component.
15121595
15131596Limit the Number of Requests
15141597----------------------------
@@ -2494,5 +2577,6 @@ body::
24942577.. _`idempotent method` : https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Idempotent_methods
24952578.. _`SSRF` : https://portswigger.net/web-security/ssrf
24962579.. _`RFC 6570` : https://www.rfc-editor.org/rfc/rfc6570
2580+ .. _`RFC 9111` : https://www.rfc-editor.org/rfc/rfc9111
24972581.. _`HAR` : https://w3c.github.io/web-performance/specs/HAR/Overview.html
24982582.. _`the Cookie HTTP request header` : https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cookie
0 commit comments