@@ -36,7 +36,7 @@ def __new__(cls, chain_endpoint: str):
36
36
cls ._instances [chain_endpoint ] = instance
37
37
return instance
38
38
39
- async def __call__ (self , chain , func , args , kwargs ) -> Optional [Any ]:
39
+ async def __call__ (self , chain , other_self , func , args , kwargs ) -> Optional [Any ]:
40
40
if not self ._db :
41
41
_ensure_dir ()
42
42
self ._db = await aiosqlite .connect (CACHE_LOCATION )
@@ -65,26 +65,28 @@ async def __call__(self, chain, func, args, kwargs) -> Optional[Any]:
65
65
);
66
66
END;"""
67
67
)
68
- key = pickle .dumps ((args , kwargs ))
68
+ await self ._db .commit ()
69
+ key = pickle .dumps ((args , kwargs or None ))
69
70
try :
70
71
cursor : aiosqlite .Cursor = await self ._db .execute (
71
72
f"SELECT value FROM { table_name } WHERE key=? AND chain=?" ,
72
73
(key , chain ),
73
74
)
74
75
result = await cursor .fetchone ()
76
+ await cursor .close ()
75
77
if result is not None :
76
78
return pickle .loads (result [0 ])
77
79
except (pickle .PickleError , sqlite3 .Error ) as e :
78
80
logger .exception ("Cache error" , exc_info = e )
79
81
pass
80
-
81
- result = await func (* args , ** kwargs )
82
+ result = await func (other_self , * args , ** kwargs )
82
83
if not local_chain or not USE_CACHE :
83
84
# TODO use a task here
84
85
await self ._db .execute (
85
86
f"INSERT OR REPLACE INTO { table_name } (key, value, chain) VALUES (?,?,?)" ,
86
87
(key , pickle .dumps (result ), chain ),
87
88
)
89
+ await self ._db .commit ()
88
90
return result
89
91
90
92
@@ -200,7 +202,7 @@ def decorator(func):
200
202
@cached_fetcher (max_size = maxsize )
201
203
async def inner (self , * args , ** kwargs ):
202
204
async_sql_db = AsyncSqliteDB (self .url )
203
- result = await async_sql_db (self .url , func , args , kwargs )
205
+ result = await async_sql_db (self .url , self , func , args , kwargs )
204
206
return result
205
207
206
208
return inner
0 commit comments