Browse code

modify 'async_lru_cache' to be more idiomatic

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.

Joseph Weston authored on 06/09/2017 01:00:07
Showing 1 changed files
... ...
@@ -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)