From d6eb16a398a8d3a2f58399fd40df7f212680cab0 Mon Sep 17 00:00:00 2001 From: Michael Manfre Date: Mon, 5 Dec 2022 12:55:30 -0500 Subject: Added caching and initial settings --- users/views/activitypub.py | 4 ++++ users/views/admin/__init__.py | 2 +- users/views/admin/settings.py | 49 +++++++++++++++++++++++++++++++++++++++ users/views/identity.py | 5 ++++ users/views/settings/interface.py | 6 ++++- 5 files changed, 64 insertions(+), 2 deletions(-) (limited to 'users') diff --git a/users/views/activitypub.py b/users/views/activitypub.py index cca57fb..c3395a4 100644 --- a/users/views/activitypub.py +++ b/users/views/activitypub.py @@ -9,6 +9,7 @@ from django.views.generic import View from activities.models import Post from core import exceptions +from core.decorators import cache_page from core.ld import canonicalise from core.models import Config from core.signatures import ( @@ -61,6 +62,7 @@ class NodeInfo(View): ) +@method_decorator(cache_page(), name="dispatch") class NodeInfo2(View): """ Returns the nodeinfo 2.0 response @@ -87,6 +89,7 @@ class NodeInfo2(View): ) +@method_decorator(cache_page(), name="dispatch") class Webfinger(View): """ Services webfinger requests @@ -189,6 +192,7 @@ class Inbox(View): return HttpResponse(status=202) +@method_decorator(cache_page(), name="dispatch") class SystemActorView(View): """ Special endpoint for the overall system actor diff --git a/users/views/admin/__init__.py b/users/views/admin/__init__.py index 101ca30..04e1195 100644 --- a/users/views/admin/__init__.py +++ b/users/views/admin/__init__.py @@ -17,7 +17,7 @@ from users.views.admin.hashtags import ( # noqa HashtagEdit, Hashtags, ) -from users.views.admin.settings import BasicSettings # noqa +from users.views.admin.settings import BasicSettings, TuningSettings # noqa @method_decorator(admin_required, name="dispatch") diff --git a/users/views/admin/settings.py b/users/views/admin/settings.py index a9ec78b..dc56693 100644 --- a/users/views/admin/settings.py +++ b/users/views/admin/settings.py @@ -1,4 +1,5 @@ from django.utils.decorators import method_decorator +from django.utils.safestring import mark_safe from core.models import Config from users.decorators import admin_required @@ -106,3 +107,51 @@ class BasicSettings(AdminSettingsPage): "restricted_usernames", ], } + + +cache_field_defaults = { + "min_value": 0, + "max_value": 900, + "step_size": 15, +} + + +class TuningSettings(AdminSettingsPage): + + section = "tuning" + + options = { + "cache_timeout_page_default": { + **cache_field_defaults, + "title": "Default Timeout", + "help_text": "The number of seconds to cache a rendered page", + }, + "cache_timeout_page_timeline": { + **cache_field_defaults, + "title": "Timeline Timeout", + "help_text": "The number of seconds to cache a rendered timeline page", + }, + "cache_timeout_page_post": { + **cache_field_defaults, + "title": "Individual Post Timeout", + "help_text": mark_safe( + "The number of seconds to cache a rendered individual Post page
Note: This includes the JSON responses to other servers" + ), + }, + "cache_timeout_identity_feed": { + **cache_field_defaults, + "title": "Identity Feed Timeout", + "help_text": "The number of seconds to cache a rendered Identity RSS feed", + }, + } + + layout = { + "Rendered Page Cache": [ + "cache_timeout_page_default", + "cache_timeout_page_timeline", + "cache_timeout_page_post", + ], + "RSS Feeds": [ + "cache_timeout_identity_feed", + ], + } diff --git a/users/views/identity.py b/users/views/identity.py index 6cfcff9..d98ce9d 100644 --- a/users/views/identity.py +++ b/users/views/identity.py @@ -10,6 +10,7 @@ from django.utils.decorators import method_decorator from django.views.generic import FormView, ListView, TemplateView, View from activities.models import Post, PostInteraction +from core.decorators import per_identity_cache_page from core.ld import canonicalise from core.models import Config from users.decorators import identity_required @@ -17,6 +18,7 @@ from users.models import Domain, Follow, FollowStates, Identity, IdentityStates from users.shortcuts import by_handle_or_404 +@method_decorator(per_identity_cache_page(), name="dispatch") class ViewIdentity(ListView): """ Shows identity profile pages, and also acts as the Actor endpoint when @@ -90,6 +92,9 @@ class ViewIdentity(ListView): return context +@method_decorator( + per_identity_cache_page("cache_timeout_identity_feed"), name="__call__" +) class IdentityFeed(Feed): """ Serves a local user's Public posts as an RSS feed diff --git a/users/views/settings/interface.py b/users/views/settings/interface.py index 5c4f229..e8c73a6 100644 --- a/users/views/settings/interface.py +++ b/users/views/settings/interface.py @@ -21,7 +21,7 @@ class SettingsPage(FormView): options_class = Config.IdentityOptions template_name = "settings/settings.html" section: ClassVar[str] - options: dict[str, dict[str, str]] + options: dict[str, dict[str, str | int]] layout: dict[str, list[str]] def get_form_class(self): @@ -51,6 +51,10 @@ class SettingsPage(FormView): choices = details.get("choices") if choices: field_kwargs["widget"] = forms.Select(choices=choices) + for int_kwarg in {"min_value", "max_value", "step_size"}: + val = details.get(int_kwarg) + if val: + field_kwargs[int_kwarg] = val form_field = forms.IntegerField else: raise ValueError(f"Cannot render settings type {config_field.type_}") -- cgit v1.2.3