In addition to being shorter and clearer, the modification also
makes debugging easier. Previously, if an exception was raised
inside a function wrapped with 'async_lru_cache' then the stack
trace would include the handling of the KeyError inside
'async_lru_cache' itself.
... | ... |
@@ -49,9 +49,7 @@ def async_lru_cache(size=float('inf')): |
49 | 49 |
|
50 | 50 |
async def memoized(func, *args, **kwargs): |
51 | 51 |
key = str((args, kwargs)) |
52 |
- try: |
|
53 |
- cache[key] = cache.pop(key) |
|
54 |
- except KeyError: |
|
52 |
+ if key not in cache: |
|
55 | 53 |
if len(cache) >= size: |
56 | 54 |
cache.popitem(last=False) |
57 | 55 |
cache[key] = await func(*args, **kwargs) |