-
-
Notifications
You must be signed in to change notification settings - Fork 370
Mitigate stale cache across processes (related to #735) #746
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
Open
snaka
wants to merge
1
commit into
splitrb:main
Choose a base branch
from
snaka:add-cache-invalidation-ttl
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -4,21 +4,32 @@ module Split | |||||||||
| class Cache | ||||||||||
| def self.clear | ||||||||||
| @cache = nil | ||||||||||
| CacheInvalidator.reset | ||||||||||
| end | ||||||||||
|
|
||||||||||
| def self.fetch(namespace, key) | ||||||||||
| return yield unless Split.configuration.cache | ||||||||||
|
|
||||||||||
| # Check global invalidation | ||||||||||
| CacheInvalidator.check_and_clear_if_needed(self) | ||||||||||
|
|
||||||||||
| @cache ||= {} | ||||||||||
| @cache[namespace] ||= {} | ||||||||||
|
|
||||||||||
| value = @cache[namespace][key] | ||||||||||
| return value if value | ||||||||||
| unless value | ||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Building on top of "broken"
|
||||||||||
| value = yield | ||||||||||
| @cache[namespace][key] = value | ||||||||||
| end | ||||||||||
|
|
||||||||||
| @cache[namespace][key] = yield | ||||||||||
| value | ||||||||||
| end | ||||||||||
|
|
||||||||||
| def self.clear_key(key) | ||||||||||
| # Invalidate globally for all processes | ||||||||||
| CacheInvalidator.invalidate | ||||||||||
|
|
||||||||||
| # Clear from local cache immediately | ||||||||||
| @cache&.keys&.each do |namespace| | ||||||||||
| @cache[namespace]&.delete(key) | ||||||||||
| end | ||||||||||
|
|
||||||||||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Split | ||
| # Manages global cache invalidation across multiple processes using Redis timestamp. | ||
| # The mechanism is opt-in: it activates only when | ||
| # Split.configuration.cache_global_ts_check_interval is set. | ||
| class CacheInvalidator | ||
| class << self | ||
| # Check global timestamp and clear cache if it has been updated. | ||
| # No-op when cache_global_ts_check_interval is not configured. | ||
| # @param cache [Split::Cache] the cache instance to potentially clear | ||
| def check_and_clear_if_needed(cache) | ||
| return unless check_interval | ||
|
|
||
| now = Time.now | ||
|
|
||
| # Skip if within check interval | ||
| return if within_check_interval?(now) | ||
|
|
||
| # Get global timestamp from Redis | ||
| current_global_ts = fetch_global_timestamp | ||
|
|
||
| # Clear cache if timestamp was updated | ||
| cache.clear if timestamp_updated?(current_global_ts) | ||
|
|
||
| # Update local timestamp and check time | ||
| update_local_state(current_global_ts, now) | ||
| end | ||
|
|
||
| # Invalidate cache globally by updating the global timestamp. | ||
| # No-op when cache_global_ts_check_interval is not configured. | ||
| def invalidate | ||
| return unless check_interval | ||
|
|
||
| Split.redis.set(global_timestamp_key, Time.now.to_f) | ||
| end | ||
|
|
||
| # Reset local state (used when cache is cleared) | ||
| def reset | ||
| @global_cache_ts = nil | ||
| @last_global_ts_check = nil | ||
| end | ||
|
|
||
| private | ||
| def within_check_interval?(now) | ||
| @last_global_ts_check && (now.to_f - @last_global_ts_check.to_f) < check_interval | ||
| end | ||
|
|
||
| def fetch_global_timestamp | ||
| Split.redis.get(global_timestamp_key).to_f | ||
| end | ||
|
|
||
| def timestamp_updated?(current_global_ts) | ||
| @global_cache_ts && current_global_ts > @global_cache_ts | ||
| end | ||
|
|
||
| def update_local_state(current_global_ts, now) | ||
| @global_cache_ts = current_global_ts | ||
| @last_global_ts_check = now | ||
| end | ||
|
|
||
| def global_timestamp_key | ||
| "split:cache:global_ts" | ||
| end | ||
|
|
||
| def check_interval | ||
| Split.configuration.cache_global_ts_check_interval | ||
| end | ||
| end | ||
| end | ||
| end |
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
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
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Building on top of "broken"
Split::Cachebehavior:clearis not thread-safe, can causeundefined method '[]' for nil (NoMethodError)to be raised fromfetchexecuting in a different thread (e.g. ifclearsets@cache = nilin thread A afterfetchperformed@cache ||= {}but before@cache[namespace][key]in thread B).