Skip to content

perf: cache API key validation#139

Open
Genmin wants to merge 1 commit intoXortexAI:mainfrom
Genmin:fix/api-key-validation-cache
Open

perf: cache API key validation#139
Genmin wants to merge 1 commit intoXortexAI:mainfrom
Genmin:fix/api-key-validation-cache

Conversation

@Genmin
Copy link
Copy Markdown

@Genmin Genmin commented May 1, 2026

Summary

  • add a bounded in-memory TTL cache for active MongoDB API-key validation results keyed by SHA-256 key hash
  • avoid repeated MongoDB find_one/update_one calls while a key is cached, keeping last_used fresh in the returned copy and refreshing MongoDB on cache miss/expiry
  • clear the validation cache when API keys are created, revoked, or renamed so same-process changes take effect immediately
  • add focused tests for cache hits, cache expiry, and inactive/missing keys

Why

Authenticated endpoints call validate_api_key on every request. Before this change, each request paid for both a MongoDB lookup and a last_used write. The cache keeps successful validations hot for a short bounded window while preserving existing behavior for invalid keys and in-memory fallback mode.

Fixes #135.

Validation

  • python3 -m pytest -o addopts='' tests/test_api_key_store.py -q
  • python3 -m py_compile src/database/api_key_store.py tests/test_api_key_store.py
  • git diff --check

Note: the repo's default pytest addopts require pytest-cov in the environment; the targeted test was run with addopts disabled because pytest-cov is not installed in this checkout.

@ishaanxgupta ishaanxgupta added the enhancement New feature or request label May 1, 2026
@ved015 ved015 assigned ved015 and unassigned ved015 May 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Perf] Cache API key validation to eliminate per-request MongoDB hit

3 participants