Skip to content

Commit d42f637

Browse files
committed
Add documentation for request cache validation config.
1 parent dfcc848 commit d42f637

File tree

1 file changed

+44
-1
lines changed

1 file changed

+44
-1
lines changed

docs/internals.rst

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,19 +124,62 @@ options on the provider instance:
124124

125125
- ``cache_allowed_requests: bool = False``
126126
- ``cacheable_requests: Set[RPCEndpoint] = CACHEABLE_REQUESTS``
127+
- ``request_cache_validation_threshold: RequestCacheValidationThreshold = RequestCacheValidationThreshold.FINALIZED``
128+
129+
For requests that don't rely on block data (e.g., ``eth_chainId``), enabling request
130+
caching by setting the ``cache_allowed_requests`` option to ``True`` will cache all
131+
responses. This is safe to do.
132+
133+
However, for requests that rely on block data (e.g., ``eth_getBlockByNumber``), it is
134+
not safe to always cache their responses because block data can change - during a
135+
chain reorganization, for example. The ``request_cache_validation_threshold`` option
136+
allows configuring a safe threshold for caching responses that depend on block data. By
137+
default, the ``finalized`` block number is used as the validation threshold, meaning
138+
that a request's response will be cached if the block number it relies on is less than
139+
or equal to the ``finalized`` block number. If the block number exceeds the
140+
``finalized`` block number, the response won't be cached.
141+
142+
This behavior can be modified by setting the ``request_cache_validation_threshold``
143+
option to ``RequestCacheValidationThreshold.SAFE``, which uses the ``safe`` block as
144+
the threshold, or to ``None``, which disables cache validation and caches all
145+
requests (this is not recommended). The ``RequestCacheValidationThreshold`` enum is
146+
imported from the ``web3.utils`` module.
147+
148+
The current list of requests that are validated by this configuration before being
149+
cached is:
150+
151+
- RPC.eth_getBlockByNumber
152+
- RPC.eth_getRawTransactionByBlockNumberAndIndex
153+
- RPC.eth_getBlockTransactionCountByNumber
154+
- RPC.eth_getUncleByBlockNumberAndIndex
155+
- RPC.eth_getUncleCountByBlockNumber
156+
- RPC.eth_getBlockByHash
157+
- RPC.eth_getTransactionByHash
158+
- RPC.eth_getTransactionByBlockNumberAndIndex
159+
- RPC.eth_getTransactionByBlockHashAndIndex
160+
- RPC.eth_getBlockTransactionCountByHash
161+
- RPC.eth_getRawTransactionByBlockHashAndIndex
162+
- RPC.eth_getUncleByBlockHashAndIndex
163+
- RPC.eth_getUncleCountByBlockHash
127164

128165
.. code-block:: python
129166
130167
from web3 import Web3, HTTPProvider
168+
from web3.utils import RequestCacheValidationThreshold
131169
132170
w3 = Web3(HTTPProvider(
133171
endpoint_uri="...",
134172
135-
# optional flag to turn on cached requests, defaults to False
173+
# optional flag to turn on cached requests, defaults to ``False``
136174
cache_allowed_requests=True,
137175
138176
# optional, defaults to an internal list of deemed-safe-to-cache endpoints
139177
cacheable_requests={"eth_chainId", "eth_getBlockByNumber"},
178+
179+
# optional, defaults to ``RequestCacheValidationThreshold.FINALIZED``
180+
# can be set to ``RequestCacheValidationThreshold.SAFE`` or turned off
181+
# by setting to ``None``.
182+
request_cache_validation_threshold=RequestCacheValidationThreshold.SAFE,
140183
))
141184
142185
.. _http_retry_requests:

0 commit comments

Comments
 (0)