made things prettier

This commit is contained in:
2026-02-22 01:16:20 +01:00
parent 076293f80d
commit 7564da1148
10 changed files with 388 additions and 626 deletions

View File

@@ -1,11 +1,12 @@
import os
import logging
from cachetools import TTLCache, cached
from eventlet.semaphore import Semaphore
from flask import request, session, redirect, url_for, current_app, abort, make_response
from typing import cast
from itsdangerous import BadSignature, URLSafeSerializer
from xkcdpass import xkcd_password
from functools import wraps, lru_cache
from functools import wraps
from urllib.parse import urlparse, urljoin
from .models import Deck, InfoBanner, Token, db, Course
@@ -13,7 +14,8 @@ from .models import Deck, InfoBanner, Token, db, Course
# Create a module-level logger per Flask/Python standards
logger = logging.getLogger(__name__)
_deck_cache = TTLCache(maxsize=1000, ttl=10)
_deck_cache_lock = Semaphore()
def init_password_generator():
@@ -190,12 +192,14 @@ def token_required(f):
return decorated_function
# Cache the last 1000 lookups per worker.
@lru_cache(maxsize=1000)
@cached(_deck_cache, lock=_deck_cache_lock)
def get_cached_deck_info(token_id, course_folder, deck_slug):
"""
Returns a dictionary of primitive types for route authorization and routing.
Never return raw SQLAlchemy models from an lru_cache!
Never return raw SQLAlchemy models from a cache!
"""
# 1. Fetch Token
token = db.session.get(Token, token_id)
@@ -221,7 +225,6 @@ def get_cached_deck_info(token_id, course_folder, deck_slug):
# 4. Check Authorization
is_unlocked = deck_slug in [d.deck for d in token.decks]
# Return only primitive data (bools, strings) so the cache is thread-safe!
return {
"auth": is_unlocked,
"course_exists": True,