From 2129db650031dec7548bb445ec2da6e332dbc6ed Mon Sep 17 00:00:00 2001 From: Nyasha Zishiri <23294558@sun.ac.za> Date: Sat, 1 Feb 2025 11:55:31 +0200 Subject: [PATCH 1/2] - Server-side caching --- app/routes/catalog.py | 3 ++- app/routes/utils.py | 2 ++ config.py | 1 + 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/app/routes/catalog.py b/app/routes/catalog.py index 21cdcff..9c65975 100644 --- a/app/routes/catalog.py +++ b/app/routes/catalog.py @@ -7,6 +7,7 @@ import requests from flask import Blueprint, abort, url_for, request, Request +import config from . import mal_client, MAL_ID_PREFIX from .auth import get_token from .manifest import MANIFEST @@ -118,7 +119,7 @@ def addon_catalog(user_id: str, catalog_type: str, catalog_id: str, offset: str meta = _mal_to_meta(anime_item, catalog_type=catalog_type, catalog_id=catalog_id, transport_url=_get_transport_url(request, user_id)) meta_previews.append(meta) - return respond_with({'metas': meta_previews}, ttl=30) + return respond_with({'metas': meta_previews}, ttl=config.CATALOG_CACHE_EXPIRE) except ValueError as e: return respond_with({'metas': [], 'message': str(e)}), 400 except requests.HTTPError as e: diff --git a/app/routes/utils.py b/app/routes/utils.py index 63291df..aa54fa6 100644 --- a/app/routes/utils.py +++ b/app/routes/utils.py @@ -38,4 +38,6 @@ def respond_with(data, ttl: int = 0) -> Response: if ttl > 0: resp.cache_control.public = True resp.cache_control.max_age = ttl + resp.cache_control.s_maxage = ttl + return resp diff --git a/config.py b/config.py index 0962f28..22a4bd3 100644 --- a/config.py +++ b/config.py @@ -41,3 +41,4 @@ class Config: STREAM_CACHE_SIZE = 20000 META_CACHE_EXPIRE = 3 * 24 * 60 * 60 STREAM_CACHE_EXPIRE = 3 * 60 * 60 +CATALOG_CACHE_EXPIRE = 60 From 9f078df26aede6531cb1d94efc4ec2a129d82404 Mon Sep 17 00:00:00 2001 From: Nyasha Zishiri <23294558@sun.ac.za> Date: Sat, 1 Feb 2025 13:10:05 +0200 Subject: [PATCH 2/2] - Server-side caching --- app/routes/catalog.py | 3 ++- app/routes/utils.py | 3 +++ config.py | 1 + 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/app/routes/catalog.py b/app/routes/catalog.py index 21cdcff..9c65975 100644 --- a/app/routes/catalog.py +++ b/app/routes/catalog.py @@ -7,6 +7,7 @@ import requests from flask import Blueprint, abort, url_for, request, Request +import config from . import mal_client, MAL_ID_PREFIX from .auth import get_token from .manifest import MANIFEST @@ -118,7 +119,7 @@ def addon_catalog(user_id: str, catalog_type: str, catalog_id: str, offset: str meta = _mal_to_meta(anime_item, catalog_type=catalog_type, catalog_id=catalog_id, transport_url=_get_transport_url(request, user_id)) meta_previews.append(meta) - return respond_with({'metas': meta_previews}, ttl=30) + return respond_with({'metas': meta_previews}, ttl=config.CATALOG_CACHE_EXPIRE) except ValueError as e: return respond_with({'metas': [], 'message': str(e)}), 400 except requests.HTTPError as e: diff --git a/app/routes/utils.py b/app/routes/utils.py index 63291df..48e5204 100644 --- a/app/routes/utils.py +++ b/app/routes/utils.py @@ -38,4 +38,7 @@ def respond_with(data, ttl: int = 0) -> Response: if ttl > 0: resp.cache_control.public = True resp.cache_control.max_age = ttl + resp.cache_control.s_maxage = ttl + resp.vary = "Accept-Encoding" + return resp diff --git a/config.py b/config.py index 0962f28..22a4bd3 100644 --- a/config.py +++ b/config.py @@ -41,3 +41,4 @@ class Config: STREAM_CACHE_SIZE = 20000 META_CACHE_EXPIRE = 3 * 24 * 60 * 60 STREAM_CACHE_EXPIRE = 3 * 60 * 60 +CATALOG_CACHE_EXPIRE = 60