2
2
import os
3
3
import pickle
4
4
import sqlite3
5
+ from pathlib import Path
5
6
import asyncstdlib as a
6
7
7
8
USE_CACHE = True if os .getenv ("NO_CACHE" ) != "1" else False
14
15
)
15
16
16
17
18
+ def _ensure_dir ():
19
+ path = Path (CACHE_LOCATION ).parent
20
+ if not path .exists ():
21
+ path .mkdir (parents = True , exist_ok = True )
22
+
23
+
17
24
def _get_table_name (func ):
18
25
"""Convert "ClassName.method_name" to "ClassName_method_name"""
19
26
return func .__qualname__ .replace ("." , "_" )
@@ -74,22 +81,28 @@ def _insert_into_cache(c, conn, table_name, key, result, chain):
74
81
pass
75
82
76
83
84
+ def _shared_inner_fn_logic (func , self , args , kwargs ):
85
+ _ensure_dir ()
86
+ conn = sqlite3 .connect (CACHE_LOCATION )
87
+ c = conn .cursor ()
88
+ table_name = _get_table_name (func )
89
+ _create_table (c , conn , table_name )
90
+ key = pickle .dumps ((args , kwargs ))
91
+ chain = self .url
92
+ if not (local_chain := _check_if_local (chain )) or not USE_CACHE :
93
+ result = _retrieve_from_cache (c , table_name , key , chain )
94
+ else :
95
+ result = None
96
+ return c , conn , table_name , key , result , chain , local_chain
97
+
98
+
77
99
def sql_lru_cache (maxsize = None ):
78
100
def decorator (func ):
79
- conn = sqlite3 .connect (CACHE_LOCATION )
80
- c = conn .cursor ()
81
- table_name = _get_table_name (func )
82
- _create_table (c , conn , table_name )
83
-
84
101
@functools .lru_cache (maxsize = maxsize )
85
102
def inner (self , * args , ** kwargs ):
86
- c = conn .cursor ()
87
- key = pickle .dumps ((args , kwargs ))
88
- chain = self .url
89
- if not (local_chain := _check_if_local (chain )) or not USE_CACHE :
90
- result = _retrieve_from_cache (c , table_name , key , chain )
91
- if result is not None :
92
- return result
103
+ c , conn , table_name , key , result , chain , local_chain = (
104
+ _shared_inner_fn_logic (func , self , args , kwargs )
105
+ )
93
106
94
107
# If not in DB, call func and store in DB
95
108
result = func (self , * args , ** kwargs )
@@ -106,21 +119,11 @@ def inner(self, *args, **kwargs):
106
119
107
120
def async_sql_lru_cache (maxsize = None ):
108
121
def decorator (func ):
109
- conn = sqlite3 .connect (CACHE_LOCATION )
110
- c = conn .cursor ()
111
- table_name = _get_table_name (func )
112
- _create_table (c , conn , table_name )
113
-
114
122
@a .lru_cache (maxsize = maxsize )
115
123
async def inner (self , * args , ** kwargs ):
116
- c = conn .cursor ()
117
- key = pickle .dumps ((args , kwargs ))
118
- chain = self .url
119
-
120
- if not (local_chain := _check_if_local (chain )) or not USE_CACHE :
121
- result = _retrieve_from_cache (c , table_name , key , chain )
122
- if result is not None :
123
- return result
124
+ c , conn , table_name , key , result , chain , local_chain = (
125
+ _shared_inner_fn_logic (func , self , args , kwargs )
126
+ )
124
127
125
128
# If not in DB, call func and store in DB
126
129
result = await func (self , * args , ** kwargs )
0 commit comments