neddit

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

commit
220d6dad6099c0a6966562d6d0fd8d612c3e5643
parent
d17c3570e02557950f6365985844d649e13f5646
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2023-10-03 04:13
async subreddit cache

Diffstat

M neddit.py 26 ++++++++++++++++++++------

1 files changed, 20 insertions, 6 deletions


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

@@ -12,6 +12,20 @@ env = jinja2.Environment(
   12    12 )
   13    13 
   14    14 
   -1    15 def async_cache(maxsize=128):
   -1    16     cache = {}
   -1    17     def decorator(func):
   -1    18         async def wrapper(*args, **kwargs):
   -1    19             key = functools._make_key(args, kwargs, False)
   -1    20             if key not in cache:
   -1    21                 if len(cache) >= maxsize:
   -1    22                     del cache[next(iter(cache))]
   -1    23                 cache[key] = await func(*args, **kwargs)
   -1    24             return cache[key]
   -1    25         return wrapper
   -1    26     return decorator
   -1    27 
   -1    28 
   15    29 def relative_datetime(value):
   16    30     dt = datetime.fromtimestamp(value)
   17    31     delta = datetime.now() - dt
@@ -54,7 +68,7 @@ def dash_resize(url):
   54    68 env.filters['dash_resize'] = dash_resize
   55    69 
   56    70 
   57    -1 def fetch(url, **params):
   -1    71 async def fetch(url, **params):
   58    72     r = requests.get(
   59    73         url,
   60    74         headers={'User-agent': 'neddit'},
@@ -67,9 +81,9 @@ def fetch(url, **params):
   67    81     return r.json()
   68    82 
   69    83 
   70    -1 @functools.lru_cache()
   71    -1 def fetch_subreddit(name):
   72    -1     return fetch('https://www.reddit.com/r/%s/about.json' % name)
   -1    84 @async_cache()
   -1    85 async def fetch_subreddit(name):
   -1    86     return await fetch('https://www.reddit.com/r/%s/about.json' % name)
   73    87 
   74    88 
   75    89 async def generic(request):
@@ -80,9 +94,9 @@ async def generic(request):
   80    94 
   81    95     if path.startswith('r/'):
   82    96         subreddit = path.split('/')[1]
   83    -1         context['subreddit'] = fetch_subreddit(subreddit)
   -1    97         context['subreddit'] = await fetch_subreddit(subreddit)
   84    98 
   85    -1     a = fetch('https://www.reddit.com/%s/.json' % path, **request.query)
   -1    99     a = await fetch('https://www.reddit.com/%s/.json' % path, **request.query)
   86   100     if isinstance(a, list):
   87   101         tpl = env.get_template('comments.html')
   88   102         context['post'] = a[0]['data']['children'][0]