Skip to content

feat: context-aware cache keys (session_context_keys)#24

Merged
nogueira-raphael merged 4 commits intomainfrom
feat/ariadne-example-fake-auth
Feb 15, 2026
Merged

feat: context-aware cache keys (session_context_keys)#24
nogueira-raphael merged 4 commits intomainfrom
feat/ariadne-example-fake-auth

Conversation

@nogueira-raphael
Copy link
Copy Markdown
Owner

@nogueira-raphael nogueira-raphael commented Feb 15, 2026

Summary

  • Add session_context_keys field to CacheConfig — specifies which keys from the GraphQL context dict are included in cache key generation
  • Ariadne and Strawberry adapters extract configured keys and pass them to the cache service, producing per-user cache entries
  • Fix: Ariadne handler now resolves context_value via get_context_for_request() before cache lookup (previously it was None at that point, so all users shared the same cache key)
  • Add fake token auth (Authorization: Bearer user-{id}) to the Ariadne example to enable per-user PRIVATE cache testing

Closes #12

Changes

  • src/cacheql/core/entities/cache_config.py — new session_context_keys: list[str] | None field
  • src/cacheql/adapters/ariadne/handler.py_extract_session_context(), early context resolution, pass context= to cache service
  • src/cacheql/adapters/strawberry/extension.py — same pattern for Strawberry adapter
  • examples/fastapi-ariadne-redis/ — fake token auth + session_context_keys=["current_user_id"]
  • tests/ — unit tests for context extraction (both adapters), cache service context isolation, integration test for per-user cache isolation

Test plan

  • make typecheck — no issues
  • 229 tests pass (make test)
  • Start the Ariadne example and verify:
    • me with Bearer user-1 → Alice (cached per user 1)
    • me with Bearer user-2 → Bob (different cache entry)
    • Same user gets cache HIT on second request

Replace hardcoded CURRENT_USER_ID with a simple token-based
user extraction from Authorization header (Bearer user-{id}).
This allows testing PRIVATE cache behavior with different users.
@codecov
Copy link
Copy Markdown

codecov Bot commented Feb 15, 2026

Codecov Report

❌ Patch coverage is 95.23810% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 88.10%. Comparing base (fb88ebc) to head (cc1340f).
⚠️ Report is 5 commits behind head on main.

Files with missing lines Patch % Lines
src/cacheql/adapters/ariadne/handler.py 88.88% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #24      +/-   ##
==========================================
+ Coverage   87.94%   88.10%   +0.16%     
==========================================
  Files          33       33              
  Lines         904      925      +21     
  Branches      136      140       +4     
==========================================
+ Hits          795      815      +20     
  Misses         87       87              
- Partials       22       23       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

PRIVATE-scoped queries (e.g. `me`) shared the same cache key for all
users. Add `session_context_keys` to CacheConfig so adapters extract
specified keys from the GraphQL context and include them in cache key
generation, producing per-user cache entries.

Closes #12
@nogueira-raphael nogueira-raphael changed the title feat: add fake token auth to Ariadne example feat: context-aware cache keys and fake token auth for Ariadne example Feb 15, 2026
Ariadne calls execute_graphql_query with context_value=None and resolves
it internally via get_context_for_request. The handler was extracting
session context from None, so all users shared the same cache key.

Now the handler resolves context_value itself when it's None, before
the cache lookup, so session_context_keys are properly extracted.
@nogueira-raphael nogueira-raphael changed the title feat: context-aware cache keys and fake token auth for Ariadne example feat: context-aware cache keys (session_context_keys) Feb 15, 2026
@nogueira-raphael nogueira-raphael merged commit f423e87 into main Feb 15, 2026
7 checks passed
@nogueira-raphael nogueira-raphael deleted the feat/ariadne-example-fake-auth branch February 15, 2026 16:35
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.

feat: add context-aware cache keys (headers in cache key)

1 participant