MONGOID-4680 implement cache_version for better Rails caching support #5874
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.
This PR adds a
#cache_version
method toMongoid::Document
instances, as well as to thehas_many
andembeds_many
proxies, which brings Mongoid closer to API parity with ActiveRecord.This also modifies
#cache_key
to better parallel how it works in ActiveRecord: ifcache_version
returns non-nil,cache_key
will not embed the timestamp in the key. Instead, the timestamp is used as the version for the entry (which reduces key churn).Note: this PR does slightly change the behavior of Mongoid when used with Rails caching (
#cache_key
will not embed the timestamp when#cache_version
is non-nil, as mentioned). We discussed this as a team and do not believe this constitutes a breaking change; in the worst case, systems that depend on caching may find at least some of their caches invalidated the first time they deploy this change.For systems where the new cache key format is a concern, you can "revert" to the original cache key format by defining a
#cache_version
ofnil
: