From a9ea9dfaeedba5be303fb4148523809ca9493c48 Mon Sep 17 00:00:00 2001 From: Ayo Ayco Date: Sun, 19 May 2024 22:01:56 +0200 Subject: [PATCH] feat: provide own module cache --- app.py | 2 ++ cache.py | 2 ++ requirements.txt | 1 + threads.py | 20 +++----------------- 4 files changed, 8 insertions(+), 17 deletions(-) create mode 100644 cache.py diff --git a/app.py b/app.py index 018ce7d..aa502c3 100755 --- a/app.py +++ b/app.py @@ -1,7 +1,9 @@ from flask import Flask from threads import threads +from cache import cache app = Flask(__name__) +cache.init_app(app, config={'CACHE_TYPE': 'SimpleCache'}) app.register_blueprint(threads, url_prefix='/') diff --git a/cache.py b/cache.py new file mode 100644 index 0000000..32a618f --- /dev/null +++ b/cache.py @@ -0,0 +1,2 @@ +from flask_caching import Cache +cache = Cache() diff --git a/requirements.txt b/requirements.txt index 74cb3fd..be37517 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ flask requests markdown +Flask-Caching \ No newline at end of file diff --git a/threads.py b/threads.py index 58ef1ef..c684ecb 100755 --- a/threads.py +++ b/threads.py @@ -3,21 +3,7 @@ import requests 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 - -########################################## +from cache import cache threads = Blueprint('threads', __name__, template_folder='templates') @@ -81,7 +67,7 @@ def api(): def api_thread(id): return fetch_thread(id) -@conditional_cache(cache.cached(timeout=60)) +@cache.cached(timeout=300) def fetch_statuses(): statuses = [] for id in thread_ids: @@ -90,7 +76,7 @@ def fetch_statuses(): statuses.append(status) return statuses -@conditional_cache(cache.cached(timeout=60)) +@cache.cached(timeout=300) def fetch_thread(id): status = requests.get(server + '/api/v1/statuses/' + id ).json() status = clean_status(status)