Add Cache-Control to feeds

This commit is contained in:
rubenwardy 2024-07-02 21:36:42 +01:00
parent d25dc2c795
commit 5f1cd080bf
3 changed files with 25 additions and 16 deletions

@ -29,11 +29,11 @@ from sqlalchemy.sql.expression import func
from app import csrf from app import csrf
from app.logic.graphs import get_package_stats, get_package_stats_for_user, get_all_package_stats 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.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, \ MinetestRelease, APIToken, PackageScreenshot, License, ContentWarning, User, PackageReview, Thread, Collection, \
PackageAlias, Language PackageAlias, Language
from app.querybuilder import QueryBuilder 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 app.utils.minetest_hypertext import html_to_minetest, package_info_as_hypertext, package_reviews_as_hypertext
from . import bp from . import bp
from .auth import is_api_authd from .auth import is_api_authd
@ -52,18 +52,6 @@ def cors_allowed(f):
return inner 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/") @bp.route("/api/packages/")
@cors_allowed @cors_allowed
@cached(300) @cached(300)

@ -20,7 +20,7 @@ from flask_babel import gettext
from app.markdown import render_markdown from app.markdown import render_markdown
from app.models import Package, PackageState, db, PackageRelease 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__) bp = Blueprint("feeds", __name__)
@ -113,36 +113,42 @@ def _atomify(feed):
@bp.route("/feeds/all.json") @bp.route("/feeds/all.json")
@cached(1800)
def all_json(): def all_json():
feed = _get_all_feed(abs_url_for("feeds.all_json")) feed = _get_all_feed(abs_url_for("feeds.all_json"))
return jsonify(feed) return jsonify(feed)
@bp.route("/feeds/all.atom") @bp.route("/feeds/all.atom")
@cached(1800)
def all_atom(): def all_atom():
feed = _get_all_feed(abs_url_for("feeds.all_atom")) feed = _get_all_feed(abs_url_for("feeds.all_atom"))
return _atomify(feed) return _atomify(feed)
@bp.route("/feeds/packages.json") @bp.route("/feeds/packages.json")
@cached(1800)
def packages_all_json(): def packages_all_json():
feed = _get_new_packages_feed(abs_url_for("feeds.packages_all_json")) feed = _get_new_packages_feed(abs_url_for("feeds.packages_all_json"))
return jsonify(feed) return jsonify(feed)
@bp.route("/feeds/packages.atom") @bp.route("/feeds/packages.atom")
@cached(1800)
def packages_all_atom(): def packages_all_atom():
feed = _get_new_packages_feed(abs_url_for("feeds.packages_all_atom")) feed = _get_new_packages_feed(abs_url_for("feeds.packages_all_atom"))
return _atomify(feed) return _atomify(feed)
@bp.route("/feeds/releases.json") @bp.route("/feeds/releases.json")
@cached(1800)
def releases_all_json(): def releases_all_json():
feed = _get_releases_feed(PackageRelease.query, abs_url_for("feeds.releases_all_json")) feed = _get_releases_feed(PackageRelease.query, abs_url_for("feeds.releases_all_json"))
return jsonify(feed) return jsonify(feed)
@bp.route("/feeds/releases.atom") @bp.route("/feeds/releases.atom")
@cached(1800)
def releases_all_atom(): def releases_all_atom():
feed = _get_releases_feed(PackageRelease.query, abs_url_for("feeds.releases_all_atom")) feed = _get_releases_feed(PackageRelease.query, abs_url_for("feeds.releases_all_atom"))
return _atomify(feed) return _atomify(feed)
@ -150,6 +156,7 @@ def releases_all_atom():
@bp.route("/packages/<author>/<name>/releases_feed.json") @bp.route("/packages/<author>/<name>/releases_feed.json")
@is_package_page @is_package_page
@cached(1800)
def releases_package_json(package: Package): def releases_package_json(package: Package):
feed = _get_releases_feed(package.releases, package.get_url("feeds.releases_package_json", absolute=True)) feed = _get_releases_feed(package.releases, package.get_url("feeds.releases_package_json", absolute=True))
return jsonify(feed) return jsonify(feed)
@ -157,6 +164,7 @@ def releases_package_json(package: Package):
@bp.route("/packages/<author>/<name>/releases_feed.atom") @bp.route("/packages/<author>/<name>/releases_feed.atom")
@is_package_page @is_package_page
@cached(1800)
def releases_package_atom(package: Package): def releases_package_atom(package: Package):
feed = _get_releases_feed(package.releases, package.get_url("feeds.releases_package_atom", absolute=True)) feed = _get_releases_feed(package.releases, package.get_url("feeds.releases_package_atom", absolute=True))
return _atomify(feed) return _atomify(feed)

@ -16,10 +16,11 @@
import datetime import datetime
import typing import typing
from functools import wraps
from urllib.parse import urljoin, urlparse, urlunparse from urllib.parse import urljoin, urlparse, urlunparse
import user_agents 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 flask_babel import LazyString, lazy_gettext
from werkzeug.datastructures import MultiDict 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("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)), (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