Skip to content

MONGOID-4680 implement cache_version for better Rails caching support #5874

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

Merged
merged 3 commits into from
Oct 7, 2024

Conversation

jamis
Copy link
Contributor

@jamis jamis commented Oct 2, 2024

This PR adds a #cache_version method to Mongoid::Document instances, as well as to the has_many and embeds_many proxies, which brings Mongoid closer to API parity with ActiveRecord.

This also modifies #cache_key to better parallel how it works in ActiveRecord: if cache_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 of nil:

def cache_version = nil

# or, for older, less glamorous, more boring and verbose rubies:
def cache_version; nil; end

@jamis jamis requested a review from comandeo-mongo October 3, 2024 14:12
@jamis jamis merged commit 4f14e14 into mongodb:master Oct 7, 2024
58 of 61 checks passed
@jamis jamis deleted the 4680-rails-caching branch October 8, 2024 19:24
@jamis jamis added the feature Adds a new feature, without breaking compatibility label Apr 10, 2025
@jamis jamis mentioned this pull request Apr 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Adds a new feature, without breaking compatibility
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants