Skip to content

Commit 41343e3

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

File tree

2 files changed

+171
-14
lines changed

2 files changed

+171
-14
lines changed

http_client.rst

Lines changed: 100 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,108 @@ 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::
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

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

reference/configuration/framework.rst

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1661,6 +1661,77 @@ 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+
.. _reference-http-client-caching-shared:
1714+
1715+
shared
1716+
"""""""
1717+
1718+
**type**: ``boolean`` **default**: ``true``
1719+
1720+
Whether the cache is shared or private. If ``true``, the cache
1721+
is `shared <https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/Caching#shared_cache>`_
1722+
(default), if ``false``, the cache is
1723+
`private <https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/Caching#private_caches>`_.
1724+
1725+
.. _reference-http-client-caching-max-ttl:
1726+
1727+
max_ttl
1728+
"""""""""
1729+
1730+
**type**: ``integer`` **default**: ``null``
1731+
1732+
The maximum time-to-live (in seconds) for cached responses. Server-provided TTLs
1733+
are capped to this value if set.
1734+
16641735
cafile
16651736
......
16661737

0 commit comments

Comments
 (0)