Skip to content

Commit 0acdc1a

Browse files
committed
Adds env var support for setting cache size
1 parent 456618d commit 0acdc1a

File tree

2 files changed

+23
-14
lines changed

2 files changed

+23
-14
lines changed

async_substrate_interface/async_substrate.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import asyncio
88
import inspect
99
import logging
10+
import os
1011
import ssl
1112
import warnings
1213
from contextlib import suppress
@@ -42,7 +43,6 @@
4243
SubstrateRequestException,
4344
ExtrinsicNotFound,
4445
BlockNotFound,
45-
MaxRetriesExceeded,
4646
StateDiscardedError,
4747
)
4848
from async_substrate_interface.protocols import Keypair
@@ -81,6 +81,10 @@
8181
logger = logging.getLogger("async_substrate_interface")
8282
raw_websocket_logger = logging.getLogger("raw_websocket")
8383

84+
# env vars dictating the cache size of the cached methods
85+
SUBSTRATE_CACHE_METHOD_SIZE = int(os.getenv("SUBSTRATE_CACHE_METHOD_SIZE", "512"))
86+
SUBSTRATE_RUNTIME_CACHE_SIZE = int(os.getenv("SUBSTRATE_RUNTIME_CACHE_SIZE", "16"))
87+
8488

8589
class AsyncExtrinsicReceipt:
8690
"""
@@ -2111,7 +2115,7 @@ async def get_metadata(self, block_hash=None) -> MetadataV15:
21112115

21122116
return runtime.metadata_v15
21132117

2114-
@cached_fetcher(max_size=512)
2118+
@cached_fetcher(max_size=SUBSTRATE_CACHE_METHOD_SIZE)
21152119
async def get_parent_block_hash(self, block_hash) -> str:
21162120
"""
21172121
Retrieves the block hash of the parent of the given block hash
@@ -2166,7 +2170,7 @@ async def get_storage_by_key(self, block_hash: str, storage_key: str) -> Any:
21662170
"Unknown error occurred during retrieval of events"
21672171
)
21682172

2169-
@cached_fetcher(max_size=16)
2173+
@cached_fetcher(max_size=SUBSTRATE_RUNTIME_CACHE_SIZE)
21702174
async def get_block_runtime_info(self, block_hash: str) -> dict:
21712175
"""
21722176
Retrieve the runtime info of given block_hash
@@ -2179,7 +2183,7 @@ async def _get_block_runtime_info(self, block_hash: str) -> dict:
21792183
response = await self.rpc_request("state_getRuntimeVersion", [block_hash])
21802184
return response.get("result")
21812185

2182-
@cached_fetcher(max_size=512)
2186+
@cached_fetcher(max_size=SUBSTRATE_CACHE_METHOD_SIZE)
21832187
async def get_block_runtime_version_for(self, block_hash: str):
21842188
"""
21852189
Retrieve the runtime version of the parent of a given block_hash
@@ -2494,7 +2498,7 @@ async def rpc_request(
24942498
else:
24952499
raise SubstrateRequestException(result[payload_id][0])
24962500

2497-
@cached_fetcher(max_size=512)
2501+
@cached_fetcher(max_size=SUBSTRATE_CACHE_METHOD_SIZE)
24982502
async def get_block_hash(self, block_id: int) -> str:
24992503
"""
25002504
Retrieves the hash of the specified block number
@@ -4022,19 +4026,19 @@ class DiskCachedAsyncSubstrateInterface(AsyncSubstrateInterface):
40224026
Experimental new class that uses disk-caching in addition to memory-caching for the cached methods
40234027
"""
40244028

4025-
@async_sql_lru_cache(maxsize=512)
4029+
@async_sql_lru_cache(maxsize=SUBSTRATE_CACHE_METHOD_SIZE)
40264030
async def get_parent_block_hash(self, block_hash):
40274031
return await self._get_parent_block_hash(block_hash)
40284032

4029-
@async_sql_lru_cache(maxsize=16)
4033+
@async_sql_lru_cache(maxsize=SUBSTRATE_RUNTIME_CACHE_SIZE)
40304034
async def get_block_runtime_info(self, block_hash: str) -> dict:
40314035
return await self._get_block_runtime_info(block_hash)
40324036

4033-
@async_sql_lru_cache(maxsize=512)
4037+
@async_sql_lru_cache(maxsize=SUBSTRATE_CACHE_METHOD_SIZE)
40344038
async def get_block_runtime_version_for(self, block_hash: str):
40354039
return await self._get_block_runtime_version_for(block_hash)
40364040

4037-
@async_sql_lru_cache(maxsize=512)
4041+
@async_sql_lru_cache(maxsize=SUBSTRATE_CACHE_METHOD_SIZE)
40384042
async def get_block_hash(self, block_id: int) -> str:
40394043
return await self._get_block_hash(block_id)
40404044

async_substrate_interface/sync_substrate.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import functools
22
import logging
3+
import os
34
import socket
45
from hashlib import blake2b
56
from typing import Optional, Union, Callable, Any
@@ -55,6 +56,10 @@
5556
logger = logging.getLogger("async_substrate_interface")
5657
raw_websocket_logger = logging.getLogger("raw_websocket")
5758

59+
# env vars dictating the cache size of the cached methods
60+
SUBSTRATE_CACHE_METHOD_SIZE = int(os.getenv("SUBSTRATE_CACHE_METHOD_SIZE", "512"))
61+
SUBSTRATE_RUNTIME_CACHE_SIZE = int(os.getenv("SUBSTRATE_RUNTIME_CACHE_SIZE", "16"))
62+
5863

5964
class ExtrinsicReceipt:
6065
"""
@@ -1668,7 +1673,7 @@ def get_metadata(self, block_hash=None) -> MetadataV15:
16681673

16691674
return runtime.metadata_v15
16701675

1671-
@functools.lru_cache(maxsize=512)
1676+
@functools.lru_cache(maxsize=SUBSTRATE_CACHE_METHOD_SIZE)
16721677
def get_parent_block_hash(self, block_hash):
16731678
block_header = self.rpc_request("chain_getHeader", [block_hash])
16741679

@@ -1708,7 +1713,7 @@ def get_storage_by_key(self, block_hash: str, storage_key: str) -> Any:
17081713
"Unknown error occurred during retrieval of events"
17091714
)
17101715

1711-
@functools.lru_cache(maxsize=16)
1716+
@functools.lru_cache(maxsize=SUBSTRATE_RUNTIME_CACHE_SIZE)
17121717
def get_block_runtime_info(self, block_hash: str) -> dict:
17131718
"""
17141719
Retrieve the runtime info of given block_hash
@@ -1718,7 +1723,7 @@ def get_block_runtime_info(self, block_hash: str) -> dict:
17181723

17191724
get_block_runtime_version = get_block_runtime_info
17201725

1721-
@functools.lru_cache(maxsize=512)
1726+
@functools.lru_cache(maxsize=SUBSTRATE_CACHE_METHOD_SIZE)
17221727
def get_block_runtime_version_for(self, block_hash: str):
17231728
"""
17241729
Retrieve the runtime version of the parent of a given block_hash
@@ -1959,7 +1964,7 @@ def _make_rpc_request(
19591964

19601965
return request_manager.get_results()
19611966

1962-
@functools.lru_cache(maxsize=512)
1967+
@functools.lru_cache(maxsize=SUBSTRATE_CACHE_METHOD_SIZE)
19631968
def supports_rpc_method(self, name: str) -> bool:
19641969
"""
19651970
Check if substrate RPC supports given method
@@ -2036,7 +2041,7 @@ def rpc_request(
20362041
else:
20372042
raise SubstrateRequestException(result[payload_id][0])
20382043

2039-
@functools.lru_cache(maxsize=512)
2044+
@functools.lru_cache(maxsize=SUBSTRATE_CACHE_METHOD_SIZE)
20402045
def get_block_hash(self, block_id: int) -> str:
20412046
return self.rpc_request("chain_getBlockHash", [block_id])["result"]
20422047

0 commit comments

Comments
 (0)