From 5f1cd080bfc013da53aad930f180dcf456543767 Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Tue, 2 Jul 2024 21:36:42 +0100 Subject: [PATCH] Add Cache-Control to feeds --- app/blueprints/api/endpoints.py | 16 ++-------------- app/blueprints/feeds/__init__.py | 10 +++++++++- app/utils/flask.py | 15 ++++++++++++++- 3 files changed, 25 insertions(+), 16 deletions(-) diff --git a/app/blueprints/api/endpoints.py b/app/blueprints/api/endpoints.py index 56fbd4d7..981e4aea 100644 --- a/app/blueprints/api/endpoints.py +++ b/app/blueprints/api/endpoints.py @@ -29,11 +29,11 @@ from sqlalchemy.sql.expression import func from app import csrf from app.logic.graphs import get_package_stats, get_package_stats_for_user, get_all_package_stats from app.markdown import render_markdown -from app.models import Tag, PackageState, PackageType, Package, db, PackageRelease, Permission, ForumTopic, \ +from app.models import Tag, PackageState, PackageType, Package, db, PackageRelease, Permission, \ MinetestRelease, APIToken, PackageScreenshot, License, ContentWarning, User, PackageReview, Thread, Collection, \ PackageAlias, Language from app.querybuilder import QueryBuilder -from app.utils import is_package_page, get_int_or_abort, url_set_query, abs_url, is_yes, get_request_date +from app.utils import is_package_page, get_int_or_abort, url_set_query, abs_url, is_yes, get_request_date, cached from app.utils.minetest_hypertext import html_to_minetest, package_info_as_hypertext, package_reviews_as_hypertext from . import bp from .auth import is_api_authd @@ -52,18 +52,6 @@ def cors_allowed(f): return inner -def cached(max_age: int): - def decorator(f): - @wraps(f) - def inner(*args, **kwargs): - res: Response = f(*args, **kwargs) - res.cache_control.max_age = max_age - return res - return inner - - return decorator - - @bp.route("/api/packages/") @cors_allowed @cached(300) diff --git a/app/blueprints/feeds/__init__.py b/app/blueprints/feeds/__init__.py index d105ffab..c9d6235f 100644 --- a/app/blueprints/feeds/__init__.py +++ b/app/blueprints/feeds/__init__.py @@ -20,7 +20,7 @@ from flask_babel import gettext from app.markdown import render_markdown from app.models import Package, PackageState, db, PackageRelease -from app.utils import is_package_page, abs_url_for +from app.utils import is_package_page, abs_url_for, cached bp = Blueprint("feeds", __name__) @@ -113,36 +113,42 @@ def _atomify(feed): @bp.route("/feeds/all.json") +@cached(1800) def all_json(): feed = _get_all_feed(abs_url_for("feeds.all_json")) return jsonify(feed) @bp.route("/feeds/all.atom") +@cached(1800) def all_atom(): feed = _get_all_feed(abs_url_for("feeds.all_atom")) return _atomify(feed) @bp.route("/feeds/packages.json") +@cached(1800) def packages_all_json(): feed = _get_new_packages_feed(abs_url_for("feeds.packages_all_json")) return jsonify(feed) @bp.route("/feeds/packages.atom") +@cached(1800) def packages_all_atom(): feed = _get_new_packages_feed(abs_url_for("feeds.packages_all_atom")) return _atomify(feed) @bp.route("/feeds/releases.json") +@cached(1800) def releases_all_json(): feed = _get_releases_feed(PackageRelease.query, abs_url_for("feeds.releases_all_json")) return jsonify(feed) @bp.route("/feeds/releases.atom") +@cached(1800) def releases_all_atom(): feed = _get_releases_feed(PackageRelease.query, abs_url_for("feeds.releases_all_atom")) return _atomify(feed) @@ -150,6 +156,7 @@ def releases_all_atom(): @bp.route("/packages///releases_feed.json") @is_package_page +@cached(1800) def releases_package_json(package: Package): feed = _get_releases_feed(package.releases, package.get_url("feeds.releases_package_json", absolute=True)) return jsonify(feed) @@ -157,6 +164,7 @@ def releases_package_json(package: Package): @bp.route("/packages///releases_feed.atom") @is_package_page +@cached(1800) def releases_package_atom(package: Package): feed = _get_releases_feed(package.releases, package.get_url("feeds.releases_package_atom", absolute=True)) return _atomify(feed) diff --git a/app/utils/flask.py b/app/utils/flask.py index 6feca7c1..82d8dd8e 100644 --- a/app/utils/flask.py +++ b/app/utils/flask.py @@ -16,10 +16,11 @@ import datetime import typing +from functools import wraps from urllib.parse import urljoin, urlparse, urlunparse import user_agents -from flask import request, abort, url_for +from flask import request, abort, url_for, Response from flask_babel import LazyString, lazy_gettext from werkzeug.datastructures import MultiDict @@ -162,3 +163,15 @@ def get_daterange_options() -> typing.List[typing.Tuple[LazyString, str]]: (lazy_gettext("Year to date"), url_set_query(start=year_start, end=now.isoformat())), (lazy_gettext("Last year"), url_set_query(start=last_year_start, end=last_year_end)), ] + + +def cached(max_age: int): + def decorator(f): + @wraps(f) + def inner(*args, **kwargs): + res: Response = f(*args, **kwargs) + res.cache_control.max_age = max_age + return res + return inner + + return decorator