Repository navigation
feat: cache commonly hit, rarely changing endpoints - #81
Merged
Merged
Conversation
Adds response caching to the hot, rarely-changing GET endpoints called by the browser extension on every load: `/`, `/user/permissions` and `/user/usage`. This avoids re-running the underlying DB aggregation on each request. Caching is built on `cachetools` (already a dependency) via a small `cached_endpoint` decorator with a per-endpoint TTLCache. User-scoped endpoints are keyed by the authenticated user's email so one user is never served another user's cached response; a regression test asserts this isolation. The auth dependency still runs on every request, so cache hits never bypass authentication, and endpoint errors (e.g. the inactive-user 403) are never cached. Closes bellingcat#50 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
msramalho
approved these changes
Jul 13, 2026
msramalho
left a comment
Contributor
There was a problem hiding this comment.
made a minor change to avoid duplicate db requests over different threads (low impact so initial PR was pretty good already).
Thanks for taking the time to submit this :)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #50. (cc @michplunkett — you'd mentioned taking this one; happy to hand off or collaborate if you're still on it.)
Adds response caching to the hot GET endpoints the extension hits on every load:
/,/user/permissions,/user/usage, avoiding a repeated DB aggregation on each request.Implementation note / open question: the issue links
fastapi-cache, but I built this oncachetools, which is already a dependency here.fastapi-cachewould add a new dependency, need lifespan backend setup, and require a custom per-user key builder plus async endpoints to get the same isolation. If you'd preferfastapi-cache(e.g. for a Redis-backed cache shared across workers), I'm glad to redo it that way — just let me know.Safety:
403) are never cached.Caches are in-process per worker with short TTLs (usage 60s, permissions/home 300s), matching the issue's note that these values "won't change every minute". Full suite passes with 8 new tests;
ruff/isortclean.