Skip to content

Commit 2b3bec4

Browse files
committed
Don't cache local chain
1 parent a98ef4c commit 2b3bec4

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

async_substrate_interface/utils/cache.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
import sqlite3
55
import asyncstdlib as a
66

7+
USE_CACHE = True if os.getenv("NO_CACHE") != "1" else False
78
CACHE_LOCATION = (
89
os.path.expanduser("~/.cache/async-substrate_interface")
9-
if os.getenv("NO_CACHE") != "1"
10+
if USE_CACHE
1011
else ":memory:"
1112
)
1213

@@ -18,6 +19,10 @@ def _get_table_name(func):
1819
return func.__qualname__.replace(".", "_")
1920

2021

22+
def _check_if_local(chain: str) -> bool:
23+
return any([x in chain for x in ["127.0.0.1", "localhost", "0.0.0.0"]])
24+
25+
2126
def _create_table(c, conn, table_name):
2227
c.execute(
2328
f"CREATE TABLE IF NOT EXISTS {table_name} (key BLOB PRIMARY KEY, value BLOB, chain TEXT)"
@@ -61,14 +66,16 @@ def inner(self, *args, **kwargs):
6166
c = conn.cursor()
6267
key = pickle.dumps((args, kwargs))
6368
chain = self.url
64-
65-
result = _retrieve_from_cache(c, table_name, key, chain)
66-
if result is not None:
67-
return result
69+
if not (local_chain := _check_if_local(chain)) or not USE_CACHE:
70+
result = _retrieve_from_cache(c, table_name, key, chain)
71+
if result is not None:
72+
return result
6873

6974
# If not in DB, call func and store in DB
7075
result = func(self, *args, **kwargs)
71-
_insert_into_cache(c, conn, table_name, key, result, chain)
76+
77+
if not local_chain or not USE_CACHE:
78+
_insert_into_cache(c, conn, table_name, key, result, chain)
7279

7380
return result
7481

@@ -87,13 +94,15 @@ async def inner(self, *args, **kwargs):
8794
key = pickle.dumps((args, kwargs))
8895
chain = self.url
8996

90-
result = _retrieve_from_cache(c, table_name, key, chain)
91-
if result is not None:
92-
return result
97+
if not (local_chain := _check_if_local(chain)) or not USE_CACHE:
98+
result = _retrieve_from_cache(c, table_name, key, chain)
99+
if result is not None:
100+
return result
93101

94102
# If not in DB, call func and store in DB
95103
result = await func(self, *args, **kwargs)
96-
_insert_into_cache(c, conn, table_name, key, result, chain)
104+
if not local_chain or not USE_CACHE:
105+
_insert_into_cache(c, conn, table_name, key, result, chain)
97106

98107
return result
99108

0 commit comments

Comments
 (0)