Caching JSON-RPC Provider
Overview
Providers include internal caching mechanisms to improve performance and reduce redundant network requests. Each provider maintains its own independent cache. This section explains how caching works and how to utilize it effectively.
LRU Cache Behavior
An LRU (Least Recently Used) cache is implemented for frequently accessed data. When the cache reaches its maximum capacity, the least recently accessed items are automatically evicted.
Cached Data Types
| Data Type | Cache Duration | Invalidation |
|---|---|---|
| Gas Parameters | ~1 block | On new block or timeout |
| Challenge | Until expiry | Automatic |
| P2OP Addresses | Long-term | Manual |
| CSV1 Addresses | Long-term | Manual |
| Chain ID | Permanent | Never |
Gas Parameters Cache
Gas parameters are cached to reduce RPC calls.
// First call fetches from network
const gasParams1 = await provider.gasParameters();
// Second call returns cached value (if recent)
const gasParams2 = await provider.gasParameters();
// Cache refreshes when data becomes staleGas Parameters Cache Behavior
| Scenario | Behavior |
|---|---|
| First request | Fetch from network, cache result |
| Subsequent request | Return cached value |
| On new block or after a few seconds since last fetch | Cache is invalidated |
Challenge Cache
Transaction challenges are cached with expiration tracking.
// Get current challenge
const challenge = await provider.getChallenge();
// If called again before expiry, returns cached
const challenge2 = await provider.getChallenge(); // Same challenge
Challenge Cache Behavior
| Scenario | Behavior |
|---|---|
| First request | Fetch from network, cache result |
| Within expiry | Return cached challenge |
| After expiry | Fetch new challenge |
| On error | Clear cache, retry |
P2OP Address Cache
P2OP (Pay-to-OPNet) addresses are cached to avoid repeated conversions.
// First lookup converts and caches
const p2op1 = contract.p2op;
// Subsequent lookups use cached value
const p2op2 = contract.p2op; // From cacheP2OP Address Cache Behavior
| Scenario | Behavior |
|---|---|
| First request | Convert to P2OP, cache result |
| Subsequent request | Return cached address |
CSV1 Address Cache
CSV1 (CheckSequenceVerify v1) addresses are cached per address.
// Get CSV1 address
const csv1 = provider.getCSV1ForAddress(address);
// Subsequent calls return cached
const csv1Again = provider.getCSV1ForAddress(address); // From cacheCSV1 Address Cache Behavior
| Scenario | Behavior |
|---|---|
| First request | Fetch from network, cache result |
| Subsequent request | Return cached value |
Performance Considerations
Benefits of Caching
- Reduced Network Calls: Fewer RPC requests.
- Lower Latency: Instant responses for cached data.
- Bandwidth Savings: Less data transferred.
- Node Relief: Less load on OP_NET nodes.
Cache Considerations
- Memory Usage: Caches consume memory.
- Stale Data: Cached data may become outdated.
- Cache Misses: First calls always hit network.
Optimizing Cache Usage
// Good: Batch operations that use same cached data
const [balance1, balance2, balance3] = await Promise.all([
contract.balanceOf(addr1),
contract.balanceOf(addr2),
contract.balanceOf(addr3),
]);
// These share cached gas parameters
// Less optimal: Sequential calls with delays
const balance1 = await contract.balanceOf(addr1);
await sleep(60000); // Cache may expire
const balance2 = await contract.balanceOf(addr2);WebSocket vs JSON-RPC Caching
Both WebSocket and JSON-RPC providers implement their own independent caching mechanisms. WebSocket providers offer near real-time cache updates through persistent connections, while JSON-RPC providers rely on time-based polling to refresh cached data.
WebSocket Provider
- Receives real-time block notifications.
- Can invalidate gas cache immediately on new blocks.
- More up-to-date cached values.
JSON-RPC Provider
- Uses time-based cache invalidation.
- Polls for updates when needed.
- May have slightly stale data between polls.
// WebSocket: Cache updates with real-time blocks
wsProvider.subscribeToBlocks((block) => {
// Gas cache automatically refreshes
});
// JSON-RPC: Cache updates on access after timeout
const gasParams = await httpProvider.gasParameters();