Skip to content

Commit 5abd398

Browse files
committed
README on cache usage and design.
1 parent f11028a commit 5abd398

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,41 @@ async def main():
5454
asyncio.run(main())
5555
```
5656

57+
### Caching
58+
There are a few different cache types used in this library to improve the performance overall. The one with which
59+
you are probably familiar is the typical `functools.lru_cache` used in `sync_substrate.SubstrateInterface`.
60+
61+
By default, it uses a max cache size of 512 for smaller returns, and 16 for larger ones. These cache sizes are
62+
user-configurable using the respective env vars, `SUBSTRATE_CACHE_METHOD_SIZE` and `SUBSTRATE_RUNTIME_CACHE_SIZE`.
63+
64+
They are applied only on methods whose results cannot change — such as the block hash for a given block number
65+
(small, 512 default), or the runtime for a given runtime version (large, 16 default).
66+
67+
Additionally, in `AsyncSubstrateInterface`, because of its asynchronous nature, we developed our own asyncio-friendly
68+
LRU caches. The primary one is the `CachedFetcher` which wraps the same methods as `functools.lru_cache` does in
69+
`SubstrateInterface`, but the key difference here is that each request is assigned a future that is returned when the
70+
initial request completes. So, if you were to do:
71+
72+
```python
73+
bn = 5000
74+
bh1, bh2 = await asyncio.gather(
75+
asi.get_block_hash(bn),
76+
asi.get_block_hash(bn)
77+
)
78+
```
79+
it would actually only make one single network call, and return the result to both requests. Like `SubstrateInterface`,
80+
it also takes the `SUBSTRATE_CACHE_METHOD_SIZE` and `SUBSTRATE_RUNTIME_CACHE_SIZE` vars to set cache size.
81+
82+
The third and final caching mechanism we use is `async_substrate_interface.async_substrate.DiskCachedAsyncSubstrateInterface`,
83+
which functions the same as the normal `AsyncSubstrateInterface`, but that also saves this cache to the disk, so the cache
84+
is preserved between runs. This is product for a fairly nice use-case (such as `btcli`). As you may call different networks
85+
with entirely different results, this cache is keyed by the uri supplied at instantiation of the `DiskCachedAsyncSubstrateInterface`
86+
object, so `DiskCachedAsyncSubstrateInterface(network_1)` and `DiskCachedAsyncSubstrateInterface(network_2)` will not share
87+
the same on-disk cache.
88+
89+
As with the other two caches, this also takes `SUBSTRATE_CACHE_METHOD_SIZE` and `SUBSTRATE_RUNTIME_CACHE_SIZE` env vars.
90+
91+
5792
## Contributing
5893

5994
Contributions are welcome! Please open an issue or submit a pull request to the `staging` branch.

0 commit comments

Comments
 (0)