From 3bc2a0b68af15b9b00a557d3a1ff9d4f84a4790e Mon Sep 17 00:00:00 2001 From: Sebastian Kuipers Date: Wed, 11 Dec 2024 16:12:52 +0100 Subject: [PATCH 1/3] Change caching to TTL caching --- .env.example | 8 ++++---- api.py | 14 +++++++------- models/common.py | 7 ++++++- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/.env.example b/.env.example index ec0f128..b4e49f2 100644 --- a/.env.example +++ b/.env.example @@ -30,8 +30,8 @@ NEW_USER=True TECHNIC_MIGRATION=True # Caching options -# Set the cache eviction algorithm. Options are FIFO, LFU, LRU or RR. Default is LRU. -CACHE_ALGORITHM=LRU - # Set the cache maximum size. Default is 100. -CACHE_SIZE=100 \ No newline at end of file +CACHE_SIZE=100 + +# Set the cache TTL in seconds. Default is 300 seconds. +CACHE_TTL=300 \ No newline at end of file diff --git a/api.py b/api.py index f601024..ebdf825 100644 --- a/api.py +++ b/api.py @@ -1,10 +1,10 @@ from flask import Blueprint, jsonify, request -from cachetools import cached +from cachetools import cached, TTLCache from models.key import Key from models.mod import Mod from models.modpack import Modpack from models.common import solderpy_version, public_repo_url -from models.common import cache_type, cache_size +from models.common import cache_size, cache_ttl api = Blueprint("api", __name__) @@ -35,7 +35,7 @@ def verify_key(key: str = None): @api.route("/api/modpack") -@cached(cache_type(cache_size), key=lambda: str(request.args.get("cid")) + str(request.args.get('include')) + str(request.args.get('k'))) +@cached(TTLCache(cache_size, cache_ttl), key=lambda: str(request.args.get("cid")) + str(request.args.get('include')) + str(request.args.get('k'))) def modpack(): cid = request.args.get("cid") keys = request.args.get("k") @@ -53,7 +53,7 @@ def modpack(): return jsonify({"modpacks": {modpack.slug: modpack.name for modpack in modpacks}, "mirror_url": public_repo_url}) @api.route("/api/modpack/") -@cached(cache_type(cache_size), key=lambda slug: str(request.args.get("cid")) + str(request.args.get('k')) + slug) +@cached(TTLCache(cache_size, cache_ttl), key=lambda slug: str(request.args.get("cid")) + str(request.args.get('k')) + slug) def modpack_slug(slug: str): cid = request.args.get("cid") keys = request.args.get("k") @@ -71,7 +71,7 @@ def modpack_slug(slug: str): return jsonify({"error": "Modpack does not exist/Build does not exist"}), 404 @api.route("/api/modpack//") -@cached(cache_type(cache_size), key=lambda slugstring, buildstring: str(request.args.get("cid")) + str(request.args.get("include")) + str(request.args.get("k")) + slugstring + buildstring) +@cached(TTLCache(cache_size, cache_ttl), key=lambda slugstring, buildstring: str(request.args.get("cid")) + str(request.args.get("include")) + str(request.args.get("k")) + slugstring + buildstring) def modpack_slug_build(slugstring: str, buildstring: str): keys = request.args.get("k") key = Key.get_key(keys) @@ -125,7 +125,7 @@ def mod(): return jsonify({"mods": {Mods.name: Mods.pretty_name for Mods in Mods}}) @api.route("/api/mod/") -@cached(cache_type(cache_size), key=lambda name: name) +@cached(TTLCache(cache_size, cache_ttl), key=lambda name: name) def mod_name(name: str): mods = Mod.get_by_name_api(name) if not mods: @@ -137,7 +137,7 @@ def mod_name(name: str): return jsonify(res) @api.route("/api/mod//") -@cached(cache_type(cache_size), key=lambda name, version: name + version) +@cached(TTLCache(cache_size, cache_ttl), key=lambda name, version: name + version) def mod_name_version(name: str, version: str): mod = Mod.get_by_name_api(name) if not mod: diff --git a/models/common.py b/models/common.py index d91bbfc..334ea14 100644 --- a/models/common.py +++ b/models/common.py @@ -1,6 +1,5 @@ import os from dotenv import load_dotenv -from cachetools import FIFOCache, LRUCache, LFUCache, RRCache from models.database import Database @@ -80,6 +79,12 @@ print("No cache size specified, using default") cache_size = int(100) +if (os.getenv("CACHE_TTL")): + cache_tool = int(os.getenv("CACHE_TTL")) +else: + print("No cache ttl specified, using default") + cache_tool = 300 + class common: @staticmethod From 5305b19911cc42fac4b0e79bb1436e77ace57fe9 Mon Sep 17 00:00:00 2001 From: maggi373 <40539743+maggi373@users.noreply.github.com> Date: Fri, 13 Dec 2024 17:07:41 +0100 Subject: [PATCH 2/3] bump version --- models/common.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/common.py b/models/common.py index 334ea14..2d86e2e 100644 --- a/models/common.py +++ b/models/common.py @@ -4,7 +4,7 @@ from models.database import Database ## Solderpy version -solderpy_version = "1.6.0" +solderpy_version = "1.6.1" load_dotenv(".env") From 5d2cfdb37c1ee855c4cc259fea23aa79047291d2 Mon Sep 17 00:00:00 2001 From: maggi373 <40539743+maggi373@users.noreply.github.com> Date: Fri, 13 Dec 2024 17:09:38 +0100 Subject: [PATCH 3/3] update readme to reflect new cache --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 13f1b83..a088bda 100644 --- a/README.md +++ b/README.md @@ -183,13 +183,13 @@ Solder.py uploads the modfiles to a volume in the container #### Caching options -Set the cache eviction algorithm. Options are FIFO, LFU, LRU or RR. Default is LRU. +Set the cache time to live, default 300 (seconds) ```bash --e CACHE_ALGORITHM=LRU +-e CACHE_TTL=300 ``` -Set the cache maximum size. Default is 100. +Set the cache maximum size ie how much memory usage. Default is 100 (MB). ```bash -e CACHE_SIZE=100