From 3c22ad7eeca0660fe77bf173c56413b5944faf28 Mon Sep 17 00:00:00 2001 From: Ayo Ayco Date: Sun, 19 May 2024 21:01:04 +0200 Subject: [PATCH] feat: use app-provided caching --- threads.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/threads.py b/threads.py index c0957ca..58ef1ef 100755 --- a/threads.py +++ b/threads.py @@ -1,9 +1,24 @@ from flask import Blueprint, render_template import requests -from datetime import datetime, timedelta +from datetime import datetime import markdown import re +##### Use app provided cache +try: + from cache.cache import cache +except ImportError: + print(' ! ERR: Cache module not found') + +def conditional_cache(dec): + def decorator(func): + if cache is None: + return func + return dec(func) + return decorator + +########################################## + threads = Blueprint('threads', __name__, template_folder='templates') # TODO: move following to an app config or sqlite ######### @@ -66,7 +81,7 @@ def api(): def api_thread(id): return fetch_thread(id) - +@conditional_cache(cache.cached(timeout=60)) def fetch_statuses(): statuses = [] for id in thread_ids: @@ -75,6 +90,7 @@ def fetch_statuses(): statuses.append(status) return statuses +@conditional_cache(cache.cached(timeout=60)) def fetch_thread(id): status = requests.get(server + '/api/v1/statuses/' + id ).json() status = clean_status(status)