neddit

the frontend of the frontpage of the internet  https://neddit.ce9e.org
git clone https://git.ce9e.org/neddit.git

commit
244790c397f33133d3483de30453403864ac753f
parent
419e81a57f8ecf94f8143c69fec9a7676ac6a013
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2025-02-07 13:22
avoid cache misses while loading

Diffstat

M neddit.py 16 ++++++++++++----

1 files changed, 12 insertions, 4 deletions


diff --git a/neddit.py b/neddit.py

@@ -1,6 +1,7 @@
    1     1 import argparse
    2    -1 import re
   -1     2 import asyncio
    3     3 import functools
   -1     4 import re
    4     5 from datetime import datetime
    5     6 from pathlib import Path
    6     7 
@@ -23,12 +24,19 @@ def async_cache(maxsize=128):
   23    24     cache = {}
   24    25     def decorator(func):
   25    26         async def wrapper(*args, **kwargs):
   26    -1             key = functools._make_key(args, kwargs, False)
   -1    27             key = functools._make_key(args, kwargs, typed=False)
   27    28             if key not in cache:
   28    29                 if len(cache) >= maxsize:
   29    30                     del cache[next(iter(cache))]
   30    -1                 cache[key] = await func(*args, **kwargs)
   31    -1             return cache[key]
   -1    31                 future = asyncio.Future()
   -1    32                 cache[key] = future
   -1    33                 try:
   -1    34                     future.set_result(await func(*args, **kwargs))
   -1    35                 except Exception as e:
   -1    36                     future.set_exception(e)
   -1    37                     del cache[key]
   -1    38                     raise
   -1    39             return await cache[key]
   32    40         return wrapper
   33    41     return decorator
   34    42