@@ -124,19 +124,62 @@ options on the provider instance:
124
124
125
125
- ``cache_allowed_requests: bool = False ``
126
126
- ``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
127
164
128
165
.. code-block :: python
129
166
130
167
from web3 import Web3, HTTPProvider
168
+ from web3.utils import RequestCacheValidationThreshold
131
169
132
170
w3 = Web3(HTTPProvider(
133
171
endpoint_uri = " ..." ,
134
172
135
- # optional flag to turn on cached requests, defaults to False
173
+ # optional flag to turn on cached requests, defaults to `` False``
136
174
cache_allowed_requests = True ,
137
175
138
176
# optional, defaults to an internal list of deemed-safe-to-cache endpoints
139
177
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 ,
140
183
))
141
184
142
185
.. _http_retry_requests :
0 commit comments