Skip to content

Commit 40461d8

Browse files
committed
[HttpClient] Document CachingHttpClient compatible with RFC 9111
1 parent 22890da commit 40461d8

File tree

2 files changed

+165
-14
lines changed

2 files changed

+165
-14
lines changed

http_client.rst

Lines changed: 98 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
743745
Retry Failed Requests
744746
~~~~~~~~~~~~~~~~~~~~~
745747

@@ -1490,25 +1492,106 @@ Caching Requests and Responses
14901492
------------------------------
14911493

14921494
This 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
14971501
installed 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

15131596
Limit 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

reference/configuration/framework.rst

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1661,6 +1661,73 @@ If this option is a boolean value, the response is buffered when the value is
16611661
returned value is ``true`` (the closure receives as argument an array with the
16621662
response headers).
16631663

1664+
caching
1665+
.......
1666+
1667+
**type**: ``array``
1668+
1669+
This option configures the behavior of the HTTP client caching, including which
1670+
types of requests to cache and how many times. The behavior is
1671+
defined with the following options:
1672+
1673+
* :ref:`cache_pool <reference-http-client-caching-cache-pool>`
1674+
* :ref:`shared <reference-http-client-caching-shared>`
1675+
* :ref:`max_ttl <reference-http-client-caching-max-ttl>`
1676+
1677+
.. code-block:: yaml
1678+
1679+
# config/packages/framework.yaml
1680+
framework:
1681+
# ...
1682+
http_client:
1683+
# ...
1684+
default_options:
1685+
caching:
1686+
cache_pool: cache.app
1687+
shared: true
1688+
max_ttl: 86400
1689+
1690+
scoped_clients:
1691+
my_api.client:
1692+
# ...
1693+
caching:
1694+
cache_pool: my_taggable_pool
1695+
1696+
.. versionadded:: 7.4
1697+
1698+
The ``caching`` option was introduced in Symfony 7.4.
1699+
1700+
.. _reference-http-client-caching-cache-pool:
1701+
1702+
cache_pool
1703+
""""""""""
1704+
1705+
**type**: ``string``
1706+
1707+
The service ID of the cache pool used to store the cached responses. The service
1708+
must implement the :class:`Symfony\\Contracts\\Cache\\TagAwareCacheInterface`.
1709+
1710+
By default, it uses an instance of :class:`Symfony\\Component\\Cache\\Adapter\\TagAwareAdapter`
1711+
wrapping the ``cache.app`` pool.
1712+
1713+
shared
1714+
"""""""
1715+
1716+
**type**: ``boolean`` **default**: ``true``
1717+
1718+
Whether the cache is shared or private. If ``true``, the cache
1719+
is `shared <https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/Caching#shared_cache>`_
1720+
(default), if ``false``, the cache is
1721+
`private <https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/Caching#private_caches>`_.
1722+
1723+
max_ttl
1724+
"""""""""
1725+
1726+
**type**: ``integer`` **default**: ``null``
1727+
1728+
The maximum time-to-live (in seconds) for cached responses. Server-provided TTLs
1729+
are capped to this value if set.
1730+
16641731
cafile
16651732
......
16661733

0 commit comments

Comments
 (0)