Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't factor $context in to cache key #12086

Merged
merged 6 commits into from
Oct 18, 2022
Merged

Conversation

brianjhanson
Copy link
Contributor

Description

Previously we were serializing $context into the cache key for graphql queries. This was good for uniqueness but it could make the caches too unique. When the cache is first being generated, fields are collected and memoized which altered the $context variable. When two queries were run right after each other, the first cache would miss, generate the cache, alter the context and cause the second query to miss the cache as well (#11994).

This solves that by ignoring the $context all together when generating the $cacheKey. The $cacheKey is already made up of a hash of the query, serialized variables and the operation name. The $context can't be altered without a plugin so it seems overkill to include within the $cacheKey.

Worth noting that this could cause some confusing situations in development if a developer is changing the context via module or plugin but doesn't change the query. With this change and caching enabled they would get stale results.

Related issues

Fixes #11994

Previously we were serializing `$context` into the cache key for graphql queries. While this was good for uniqueness it could make the caches _too_ unique. When the cache is first being generated, fields are collected and memoized which altered the `$context` variable. When two queries were run right after each other, the first cache would miss, generate the cache, alter the context and cause the second query to miss the cache as well.

This solves that by ignoring the `$context` all together when generating the `$cacheKey`. The `$cacheKey` is already made up of a hash of the query, serialized variables and the operation name. The `$context` can't be altered without a plugin so it seems overkill to include within the `$cacheKey`.

Worth noting that this could cause some confusing situations in development if a developer is changing the context via module or plugin but doesn't change the query. With this change and caching enabled they would get stale results.
@brianjhanson brianjhanson requested a review from a team as a code owner October 11, 2022 17:42
@Wiejeben
Copy link
Contributor

Wiejeben commented Oct 12, 2022

I did some testing, everything from editing entry types, globals, adding, renaming and removing fields and the cache still seems to work as expected on my testing environment.

In case there is a situation where the context was the deciding factor I personally would consider replacing it with Craft::$app->getInfo()->configVersion for the cache key just to make sure that any configuration changes to the CMS would be picked up.

@brianjhanson
Copy link
Contributor Author

@brandonkelly this is ready for you now. Thank you @Wiejeben for testing it out! 🙏

@brandonkelly brandonkelly changed the base branch from develop to 4.3 October 18, 2022 00:34
@brandonkelly brandonkelly merged commit dbec5e6 into 4.3 Oct 18, 2022
@brandonkelly brandonkelly deleted the bugfix/11994-graphql-cache-hits branch October 18, 2022 00:38
@brandonkelly
Copy link
Member

Merged for 4.3 🎉

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.

[4.x]: GraphQL cache key generation fails on second GraphQL call
3 participants