- commit
- b81e2fdf4fe722bc68f905494376f285bdd4f068
- parent
- 71bcde960a5b30ccae8587add24f55cda2adb5f1
- Author
- Tobias Bengfort <tobias.bengfort@posteo.de>
- Date
- 2025-03-01 08:55
refactor async_cache avoid issues when cache is cleared before future resolves
Diffstat
M | dropin.py | 21 | +++++++++++---------- |
1 files changed, 11 insertions, 10 deletions
diff --git a/dropin.py b/dropin.py
@@ -178,16 +178,17 @@ def async_cache(func): 178 178 cache = {} 179 179 async def wrapper(*args, **kwargs): 180 180 key = functools._make_key(args, kwargs, typed=False)181 -1 if key not in cache or cache[key][0] < time.time():182 -1 future = asyncio.Future()183 -1 cache[key] = (time.time() + CACHE_DURATION, future)184 -1 try:185 -1 future.set_result(await func(*args, **kwargs))186 -1 except Exception as e:187 -1 future.set_exception(e)188 -1 del cache[key]189 -1 raise190 -1 return await cache[key][1]-1 181 if key in cache and cache[key][0] >= time.time(): -1 182 return await cache[key][1] -1 183 -1 184 future = asyncio.Future() -1 185 cache[key] = (time.time() + CACHE_DURATION, future) -1 186 try: -1 187 future.set_result(await func(*args, **kwargs)) -1 188 except Exception as e: -1 189 future.set_exception(e) -1 190 del cache[key] -1 191 return await future 191 192 return wrapper 192 193 193 194