Repository navigation
Add optional Active Record feature-state cache #198
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e52f74d
Add optional in-process feature-state cache
michal-granec bdca876
Document that Active Record caching is off by default
michal-granec 22e3616
Harden Active Record feature cache fills and invalidation
michal-granec b3f47f7
Make query-cache refresh specs compatible with Active Record 7.1
michal-granec afb0668
Skip stale query-cache refresh specs on Active Record 7.1
michal-granec c15a9c2
Rename cache_ttl to cache_ttl_seconds
michal-granec 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
94 changes: 94 additions & 0 deletions
94
rollout-active_record-adapter/lib/rollout/active_record/feature_cache.rb
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,94 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class Rollout | ||
| module ActiveRecord | ||
| class FeatureCache | ||
| DEFAULT_MAX_SIZE = 4096 | ||
|
|
||
| def initialize(ttl_seconds:, clock: nil, max_size: DEFAULT_MAX_SIZE) | ||
| unless ttl_seconds.is_a?(Integer) && ttl_seconds > 0 | ||
| raise ArgumentError, "cache_ttl_seconds must be an Integer > 0" | ||
| end | ||
| unless max_size.is_a?(Integer) && max_size > 0 | ||
| raise ArgumentError, "max_size must be an Integer > 0" | ||
| end | ||
|
|
||
| @ttl_seconds = ttl_seconds | ||
| @max_size = max_size | ||
| @clock = clock || -> { Process.clock_gettime(Process::CLOCK_MONOTONIC) } | ||
| @entries = {} | ||
| @generation = 0 | ||
| @mutex = Mutex.new | ||
| end | ||
|
|
||
| def generation | ||
| @mutex.synchronize { @generation } | ||
| end | ||
|
|
||
| def read(name, context: nil) | ||
| key = entry_key(name, context) | ||
| @mutex.synchronize do | ||
| entry = @entries[key] | ||
| return nil unless entry | ||
| if entry[:expires_at] <= now | ||
| @entries.delete(key) | ||
| return nil | ||
| end | ||
|
|
||
| entry[:state].deep_clone | ||
| end | ||
| end | ||
|
|
||
| def fill(generation, states, context: nil) | ||
| clones = states.map { |state| [state.name.to_s, state.deep_clone] } | ||
| @mutex.synchronize do | ||
| return if generation != @generation | ||
|
|
||
| clones.each do |name, clone| | ||
| store_locked(name, clone, context) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| def delete(*names, context: nil) | ||
| keys = names.map { |name| entry_key(name, context) } | ||
| @mutex.synchronize do | ||
| @generation += 1 | ||
| keys.each { |key| @entries.delete(key) } | ||
| end | ||
| end | ||
|
|
||
| def clear | ||
| @mutex.synchronize do | ||
| @generation += 1 | ||
| @entries.clear | ||
| end | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def store_locked(name, clone, context) | ||
| key = entry_key(name, context) | ||
| @entries.delete(key) | ||
| if @entries.size >= @max_size | ||
| prune_expired_locked | ||
| @entries.shift while @entries.size >= @max_size | ||
| end | ||
| @entries[key] = { state: clone, expires_at: now + @ttl_seconds } | ||
| end | ||
|
|
||
| def prune_expired_locked | ||
| t = now | ||
| @entries.delete_if { |_key, entry| entry[:expires_at] <= t } | ||
| end | ||
|
|
||
| def entry_key(name, context) | ||
| [context, name.to_s] | ||
| end | ||
|
|
||
| def now | ||
| @clock.call | ||
| 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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.