@@ -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,108 @@ 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 ::
15021504
1503-     $store = new Store('/path/to/cache/storage/'); 
1504-     $client = HttpClient::create(); 
1505-     $client = new CachingHttpClient($client, $store); 
1505+     The implementation is asynchronous, so the response must be consumed
1506+     (e.g., via getContent() or streaming) for caching to occur.
1507+ 
1508+ .. configuration-block ::
1509+ 
1510+     .. code-block :: yaml 
1511+ 
1512+         #  config/packages/framework.yaml 
1513+         framework : 
1514+             http_client : 
1515+                 scoped_clients : 
1516+                     example.client : 
1517+                         base_uri : ' https://example.com'  
1518+                         caching : 
1519+                             cache_pool : example_cache_pool  
1520+ 
1521+             cache : 
1522+                 pools : 
1523+                     example_cache_pool : 
1524+                         adapter : cache.adapter.redis_tag_aware  
1525+                         tags : true  
1526+ 
1527+ code-block :: xml 
1528+ 
1529+         <!--  config/packages/framework.xml -->  
1530+         <?xml  version =" 1.0"  encoding =" UTF-8"  
1531+         <container  xmlns =" http://symfony.com/schema/dic/services"  
1532+             xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"  
1533+             xmlns : framework =" http://symfony.com/schema/dic/symfony"  
1534+             xsi : schemaLocation =" http://symfony.com/schema/dic/services 
1535+                 https://symfony.com/schema/dic/services/services-1.0.xsd 
1536+                 http://symfony.com/schema/dic/symfony https://symfony.com/schema/dic/symfony/symfony-1.0.xsd"  >
1537+ 
1538+             <framework : config > 
1539+                 <framework : http-client > 
1540+                     <framework : scoped-client  name =" example.client"  
1541+                         base-uri =" https://example.com"  
1542+                     > 
1543+                         <framework : caching  cache-pool =" example_cache_pool"  
1544+                     </framework : scoped-client > 
1545+                 </framework : http-client > 
1546+ 
1547+                 <framework : cache > 
1548+                     <framework : pool  name =" example_cache_pool"  
1549+                         adapter =" cache.adapter.redis_tag_aware"  
1550+                         tags =" true"  
1551+                     /> 
1552+                 </framework : cache > 
1553+             </framework : config > 
1554+         </container > 
1555+ 
1556+ code-block :: php 
1557+ 
1558+         // config/packages/framework.php 
1559+         use Symfony\Config\FrameworkConfig; 
1560+ 
1561+         return static function (FrameworkConfig $framework): void { 
1562+             $framework->httpClient()->scopedClient('example.client') 
1563+                 ->baseUri('https://example.com') 
1564+                 ->caching() 
1565+                     ->cachePool('example_cache_pool') 
1566+                 // ... 
1567+             ; 
1568+ 
1569+             $framework->cache() 
1570+                 ->pool('example_cache_pool') 
1571+                     ->adapter('cache.adapter.redis_tag_aware') 
1572+                     ->tags(true) 
1573+                 ; 
1574+         }; 
1575+ 
1576+ code-block :: php-standalone 
1577+ 
1578+         use Symfony\Component\Cache\Adapter\FilesystemTagAwareAdapter; 
1579+         use Symfony\Component\HttpClient\CachingHttpClient; 
1580+         use Symfony\Component\HttpClient\HttpClient; 
1581+ 
1582+         $cache = new FilesystemTagAwareAdapter(); 
1583+ 
1584+         $client = HttpClient::createForBaseUri('https://example.com'); 
1585+         $cachingClient = new CachingHttpClient($client, $cache); 
1586+ 
1587+ tip ::
1588+ 
1589+     It is also highly recommended to configure a :ref: `retry strategy  <http-client-retry-failed-requests >`
1590+     to gracefully handle cache inconsistency.
15061591
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'); 
1592+ .. versionadded :: 7.4 
15091593
1510- :class: `Symfony\\ Component\\ HttpClient\\ CachingHttpClient ` accepts a third argument
1511- to set the options of the :class: `Symfony\\ Component\\ HttpKernel\\ HttpCache\\ HttpCache `.
1594+     Compliance with `RFC 9111 `_ and leveraging the
1595+     :doc: `Cache component  </components/cache >` was introduced in Symfony 7.4.
1596+     Prior to this, it used ``HttpCache `` from the HttpKernel component.
15121597
15131598Limit the Number of Requests
15141599---------------------------- 
@@ -2494,5 +2579,6 @@ body::
24942579.. _`idempotent method` : https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Idempotent_methods 
24952580.. _`SSRF` : https://portswigger.net/web-security/ssrf 
24962581.. _`RFC 6570` : https://www.rfc-editor.org/rfc/rfc6570 
2582+ .. _`RFC 9111` : https://www.rfc-editor.org/rfc/rfc9111 
24972583.. _`HAR` : https://w3c.github.io/web-performance/specs/HAR/Overview.html 
24982584.. _`the Cookie HTTP request header` : https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cookie 
0 commit comments