feat: context-aware cache keys (session_context_keys)#24
Merged
nogueira-raphael merged 4 commits intomainfrom Feb 15, 2026
Merged
feat: context-aware cache keys (session_context_keys)#24nogueira-raphael merged 4 commits intomainfrom
nogueira-raphael merged 4 commits intomainfrom
Conversation
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 Report❌ Patch coverage is
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. 🚀 New features to boost your workflow:
|
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
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.
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.
Summary
session_context_keysfield toCacheConfig— specifies which keys from the GraphQL context dict are included in cache key generationcontext_valueviaget_context_for_request()before cache lookup (previously it wasNoneat that point, so all users shared the same cache key)Authorization: Bearer user-{id}) to the Ariadne example to enable per-user PRIVATE cache testingCloses #12
Changes
src/cacheql/core/entities/cache_config.py— newsession_context_keys: list[str] | Nonefieldsrc/cacheql/adapters/ariadne/handler.py—_extract_session_context(), early context resolution, passcontext=to cache servicesrc/cacheql/adapters/strawberry/extension.py— same pattern for Strawberry adapterexamples/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 isolationTest plan
make typecheck— no issuesmake test)mewithBearer user-1→ Alice (cached per user 1)mewithBearer user-2→ Bob (different cache entry)