Skip to content

feat: cache commonly hit, rarely changing endpoints - #81

Merged
msramalho merged 2 commits into
bellingcat:mainfrom
steps-re:feat/cache-hot-endpoints
Jul 13, 2026
Merged

msramalho merged 2 commits into
bellingcat:mainfrom
steps-re:feat/cache-hot-endpoints

Conversation

@steps-re

@steps-re steps-re commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

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 on cachetools, which is already a dependency here. fastapi-cache would 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 prefer fastapi-cache (e.g. for a Redis-backed cache shared across workers), I'm glad to redo it that way — just let me know.

Safety:

  • User-scoped caches are keyed by the authenticated user's email, so responses are never shared across users (there's a dedicated regression test for this).
  • The auth dependency still runs on every request, so cache hits don't bypass authentication.
  • Endpoint errors (the inactive-user 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/isort clean.

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>
@steps-re
steps-re requested a review from msramalho as a code owner July 7, 2026 02:31

@msramalho msramalho left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 :)

@msramalho
msramalho merged commit 6c10574 into bellingcat:main Jul 13, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

test and implement fast-api cache on commonly hit endpoints

2 participants