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
Add redis cache adapter #211
Merged
Merged
Changes from 12 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
529e1b8
get adapter tests working with docker
camallen 191a0db
copy interface from dalli for redis_cache
camallen b2ee475
add testing setup / run instructions
camallen e1a578e
60 mins default ttl
camallen 118e5e8
implement redis cache remove
camallen bea39f2
mimic dalli fetch method
camallen c35ffda
implement rest of redis_cache adapter
camallen f67c1b4
don’t wrap the dalli adapter in a dalli adapter
camallen 4ace63b
require pry for debugging
camallen 61fc237
extract cache multi get method
camallen 3d1eadd
consistent formatting
camallen 5c72752
add minitest for redis_cache adapter
camallen 879f077
remove pry require as it breaks the tests
camallen 2100398
add redis uri scheme
camallen File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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 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,128 @@ | ||
require 'redis' | ||
|
||
module Flipper | ||
module Adapters | ||
# Public: Adapter that wraps another adapter with the ability to cache | ||
# adapter calls in Redis | ||
class RedisCache | ||
include ::Flipper::Adapter | ||
|
||
Version = "v1".freeze | ||
Namespace = "flipper/#{Version}".freeze | ||
FeaturesKey = "#{Namespace}/features".freeze | ||
|
||
# Private | ||
def self.key_for(key) | ||
"#{Namespace}/feature/#{key}" | ||
end | ||
|
||
# Internal | ||
attr_reader :cache | ||
|
||
# Public: The name of the adapter. | ||
attr_reader :name | ||
|
||
# Public | ||
def initialize(adapter, cache, ttl = 3600) | ||
@adapter = adapter | ||
@name = :redis_cache | ||
@cache = cache | ||
@ttl = ttl | ||
end | ||
|
||
# Public | ||
def features | ||
fetch(FeaturesKey) do | ||
@adapter.features | ||
end | ||
end | ||
|
||
# Public | ||
def add(feature) | ||
result = @adapter.add(feature) | ||
@cache.del(FeaturesKey) | ||
result | ||
end | ||
|
||
# Public | ||
def remove(feature) | ||
result = @adapter.remove(feature) | ||
@cache.del(FeaturesKey) | ||
@cache.del(key_for(feature.key)) | ||
result | ||
end | ||
|
||
# Public | ||
def clear(feature) | ||
result = @adapter.clear(feature) | ||
@cache.del(key_for(feature.key)) | ||
result | ||
end | ||
|
||
# Public | ||
def get(feature) | ||
fetch(key_for(feature.key)) do | ||
@adapter.get(feature) | ||
end | ||
end | ||
|
||
def get_multi(features) | ||
keys = features.map { |feature| feature.key } | ||
result = Hash[keys.zip(multi_cache_get(keys))] | ||
uncached_features = features.reject do |feature| | ||
result[feature.key] | ||
end | ||
|
||
if uncached_features.any? | ||
response = @adapter.get_multi(uncached_features) | ||
response.each do |key, value| | ||
set_with_ttl(key_for(key), value) | ||
result[key] = value | ||
end | ||
end | ||
result | ||
end | ||
|
||
# Public | ||
def enable(feature, gate, thing) | ||
result = @adapter.enable(feature, gate, thing) | ||
@cache.del(key_for(feature.key)) | ||
result | ||
end | ||
|
||
# Public | ||
def disable(feature, gate, thing) | ||
result = @adapter.disable(feature, gate, thing) | ||
@cache.del(key_for(feature.key)) | ||
result | ||
end | ||
|
||
private | ||
|
||
def key_for(key) | ||
self.class.key_for(key) | ||
end | ||
|
||
def fetch(key, &block) | ||
if cached = @cache.get(key) | ||
return Marshal.load(cached) | ||
else | ||
to_cache = block.call | ||
set_with_ttl(key, to_cache) | ||
to_cache | ||
end | ||
end | ||
|
||
def set_with_ttl(key, value) | ||
@cache.setex(key, @ttl, Marshal.dump(value)) | ||
end | ||
|
||
def multi_cache_get(keys) | ||
cache_keys = keys.map { |key| key_for(key) } | ||
@cache.mget(cache_keys).map do |value| | ||
value ? Marshal.load(value) : nil | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains 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 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,65 @@ | ||
require 'helper' | ||
require 'flipper/adapters/redis_cache' | ||
require 'flipper/spec/shared_adapter_specs' | ||
|
||
RSpec.describe Flipper::Adapters::RedisCache do | ||
let(:client) { | ||
options = {} | ||
|
||
if ENV['BOXEN_REDIS_URL'] | ||
options[:url] = ENV['BOXEN_REDIS_URL'] | ||
end | ||
|
||
Redis.new(options) | ||
} | ||
|
||
let(:memory_adapter) { Flipper::Adapters::Memory.new } | ||
let(:cache) { Redis.new({url: ENV.fetch('BOXEN_REDIS_URL', 'localhost:6379')}) } | ||
let(:adapter) { Flipper::Adapters::RedisCache.new(memory_adapter, cache) } | ||
let(:flipper) { Flipper.new(adapter) } | ||
|
||
subject { adapter } | ||
|
||
before do | ||
client.flushdb | ||
end | ||
|
||
it_should_behave_like 'a flipper adapter' | ||
|
||
describe "#remove" do | ||
it "expires feature" do | ||
feature = flipper[:stats] | ||
adapter.get(feature) | ||
adapter.remove(feature) | ||
expect(cache.get(described_class.key_for(feature))).to be(nil) | ||
end | ||
end | ||
|
||
describe "#get_multi" do | ||
it "warms uncached features" do | ||
stats = flipper[:stats] | ||
search = flipper[:search] | ||
other = flipper[:other] | ||
stats.enable | ||
search.enable | ||
|
||
adapter.get(stats) | ||
expect(cache.get(described_class.key_for(search))).to be(nil) | ||
expect(cache.get(described_class.key_for(other))).to be(nil) | ||
|
||
adapter.get_multi([stats, search, other]) | ||
|
||
search_cache_value, other_cache_value = [search, other].map do |f| | ||
Marshal.load(cache.get(described_class.key_for(f))) | ||
end | ||
expect(search_cache_value[:boolean]).to eq("true") | ||
expect(other_cache_value[:boolean]).to be(nil) | ||
end | ||
end | ||
|
||
describe "#name" do | ||
it "is redis_cache" do | ||
expect(subject.name).to be(:redis_cache) | ||
end | ||
end | ||
end |
This file contains 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 |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
|
||
require 'rubygems' | ||
require 'bundler' | ||
require 'pry' | ||
|
||
Bundler.setup(:default) | ||
|
||
|
This file contains 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 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 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,18 @@ | ||
require 'test_helper' | ||
require 'flipper/adapters/memory' | ||
require 'flipper/adapters/redis_cache' | ||
|
||
class DalliTest < MiniTest::Test | ||
prepend Flipper::Test::SharedAdapterTests | ||
|
||
def setup | ||
url = ENV.fetch('BOXEN_REDIS_URL', 'localhost:6379') | ||
@cache = Redis.new({url: url}).tap { |c| c.flushdb } | ||
memory_adapter = Flipper::Adapters::Memory.new | ||
@adapter = Flipper::Adapters::RedisCache.new(memory_adapter, @cache) | ||
end | ||
|
||
def teardown | ||
@cache.flushdb | ||
end | ||
end |
This file contains 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.
happy to remove this if it's not used widely