Prevent spurious SQL query execution via ActiveSupport::Cache events #188
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.
When using a simple Rails view template like
we noticed that a spurious SQL query
was executed both on cache miss and on cache hit.
Since the very point of fragment caching is to prevent this kind of queries, we investigated and found out that they were caused by
meta_request
.Rather than just stating the cache key used, events from
ActiveSupport::Cache
provide all original objects which contributed to the calculation of the cache key. This includedUser.all
from the template.When
meta_request
checks whether this object is encodable to JSONunintentionally triggers the spurious SQL query.
We fix this by sanitizing the event payload and replacing the inputs of the cache key calculation with their final result.