Skip to content

Commit

Permalink
Cache locale negotiation
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasjuhrich committed Oct 18, 2023
1 parent b18fc74 commit a57e33b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
2 changes: 1 addition & 1 deletion sipa/blueprints/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def render_meetingcal():
return render_template('meetingcal.html', meetings=meetings)


@bp_features.route("/meetings")
@bp_features.route("/meetings-fragment")
def meetings():
return render_template_string(
"""
Expand Down
23 changes: 17 additions & 6 deletions sipa/flatpages.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from __future__ import annotations

import logging
from dataclasses import dataclass, field
from functools import cached_property
from functools import cached_property, lru_cache
from operator import attrgetter
from os.path import basename, dirname, splitext

Expand All @@ -15,6 +16,17 @@
logger = logging.getLogger(__name__)


@lru_cache(maxsize=128)
def cached_negotiate_locale(
preferred_locales: tuple[str], available_locales: tuple[str]
) -> str | None:
return negotiate_locale(
preferred_locales,
available_locales,
sep="-",
)


# NB: Node is meant to be a union `Article | Category`.
@dataclass
class Node:
Expand Down Expand Up @@ -141,8 +153,8 @@ def __getattr__(self, attr: str) -> str:
.format(type(self).__name__, attr)) from e

@cached_property
def available_locales(self) -> list[str]:
return list(self.localized_pages.keys())
def available_locales(self) -> tuple[str]:
return tuple(self.localized_pages.keys())

@property
def localized_page(self) -> Page:
Expand All @@ -154,10 +166,9 @@ def localized_page(self) -> Page:
:returns: The localized page
"""
negotiated_locale = negotiate_locale(
preferred_locales(),
negotiated_locale = cached_negotiate_locale(
tuple(preferred_locales()),
self.available_locales,
sep="-",
)
if negotiated_locale is not None:
return self.localized_pages[negotiated_locale]
Expand Down

0 comments on commit a57e33b

Please sign in to comment.