@@ -55,55 +55,61 @@ def _insert_into_cache(c, conn, table_name, key, result, chain):
5555 pass
5656
5757
58- def sql_lru_cache (func , max_size = None ):
59- conn = sqlite3 .connect (CACHE_LOCATION )
60- c = conn .cursor ()
61- table_name = _get_table_name (func )
62- _create_table (c , conn , table_name )
63-
64- @functools .lru_cache (maxsize = max_size )
65- def inner (self , * args , ** kwargs ):
58+ def sql_lru_cache (max_size = None ):
59+ def decorator (func ):
60+ conn = sqlite3 .connect (CACHE_LOCATION )
6661 c = conn .cursor ()
67- key = pickle .dumps ((args , kwargs ))
68- chain = self .url
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
62+ table_name = _get_table_name (func )
63+ _create_table (c , conn , table_name )
7364
74- # If not in DB, call func and store in DB
75- result = func (self , * args , ** kwargs )
65+ @functools .lru_cache (maxsize = max_size )
66+ def inner (self , * args , ** kwargs ):
67+ c = conn .cursor ()
68+ key = pickle .dumps ((args , kwargs ))
69+ chain = self .url
70+ if not (local_chain := _check_if_local (chain )) or not USE_CACHE :
71+ result = _retrieve_from_cache (c , table_name , key , chain )
72+ if result is not None :
73+ return result
7674
77- if not local_chain or not USE_CACHE :
78- _insert_into_cache ( c , conn , table_name , key , result , chain )
75+ # If not in DB, call func and store in DB
76+ result = func ( self , * args , ** kwargs )
7977
80- return result
78+ if not local_chain or not USE_CACHE :
79+ _insert_into_cache (c , conn , table_name , key , result , chain )
8180
82- return inner
81+ return result
8382
83+ return inner
8484
85- def async_sql_lru_cache (func , max_size = None ):
86- conn = sqlite3 .connect (CACHE_LOCATION )
87- c = conn .cursor ()
88- table_name = _get_table_name (func )
89- _create_table (c , conn , table_name )
85+ return decorator
9086
91- @a .lru_cache (maxsize = max_size )
92- async def inner (self , * args , ** kwargs ):
87+
88+ def async_sql_lru_cache (max_size = None ):
89+ def decorator (func ):
90+ conn = sqlite3 .connect (CACHE_LOCATION )
9391 c = conn .cursor ()
94- key = pickle .dumps ((args , kwargs ))
95- chain = self .url
92+ table_name = _get_table_name (func )
93+ _create_table (c , conn , table_name )
94+
95+ @a .lru_cache (maxsize = max_size )
96+ async def inner (self , * args , ** kwargs ):
97+ c = conn .cursor ()
98+ key = pickle .dumps ((args , kwargs ))
99+ chain = self .url
100+
101+ if not (local_chain := _check_if_local (chain )) or not USE_CACHE :
102+ result = _retrieve_from_cache (c , table_name , key , chain )
103+ if result is not None :
104+ return result
96105
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
106+ # If not in DB, call func and store in DB
107+ result = await func ( self , * args , ** kwargs )
108+ if not local_chain or not USE_CACHE :
109+ _insert_into_cache ( c , conn , table_name , key , result , chain )
101110
102- # If not in DB, call func and store in DB
103- result = await func (self , * args , ** kwargs )
104- if not local_chain or not USE_CACHE :
105- _insert_into_cache (c , conn , table_name , key , result , chain )
111+ return result
106112
107- return result
113+ return inner
108114
109- return inner
115+ return decorator
0 commit comments