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
Dalli (memcached) adapter #132
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
require 'dalli' | ||
|
||
module Flipper | ||
module Adapters | ||
# Internal: Adapter that wraps another adapter with the ability to cache | ||
# adapter calls in Memcached using the Dalli gem. | ||
class Dalli | ||
include Flipper::Adapter | ||
FeaturesKey = :flipper_features | ||
|
||
#Internal | ||
attr_accessor :cache | ||
|
||
#Public: The name of the adapter. | ||
attr_accessor :name | ||
|
||
# Public | ||
def initialize(adapter, cache, ttl = 0) | ||
@adapter = adapter | ||
@name = adapter.name | ||
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. I'm inclined to leave this as :dalli or :cached if we go that route. I think there is value in this adapter's name being different than the adapter it is wrapping so you can instrument them separately to see cached and uncached performance. 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. Fixed by 8fd1a7d |
||
@cache = cache | ||
@ttl = ttl | ||
end | ||
|
||
# Public | ||
def features | ||
@cache.fetch(FeaturesKey, @ttl) do | ||
@adapter.features | ||
end | ||
end | ||
|
||
# Public | ||
def add(feature) | ||
result = @adapter.add(feature) | ||
@cache.delete(FeaturesKey) | ||
result | ||
end | ||
|
||
# Public | ||
def remove(feature) | ||
result = @adapter.remove(feature) | ||
@cache.delete(FeaturesKey) | ||
result | ||
end | ||
|
||
# Public | ||
def clear(feature) | ||
result = @adapter.clear(feature) | ||
@cache.delete(feature) | ||
result | ||
end | ||
|
||
# Public | ||
def get(feature) | ||
@cache.fetch(feature, @ttl) do | ||
@adapter.get(feature) | ||
end | ||
end | ||
|
||
# Public | ||
def enable(feature, gate, thing) | ||
result = @adapter.enable(feature, gate, thing) | ||
@cache.delete(feature) | ||
result | ||
end | ||
|
||
# Public | ||
def disable(feature, gate, thing) | ||
result = @adapter.disable(feature, gate, thing) | ||
@cache.delete(feature) | ||
result | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
require 'test_helper' | ||
require 'flipper/adapters/memory' | ||
require 'flipper/adapters/dalli' | ||
|
||
class DalliTest < MiniTest::Test | ||
prepend SharedAdapterTests | ||
|
||
def setup | ||
@cache = Dalli::Client.new('localhost:11211') | ||
memory_adapter = Flipper::Adapters::Memory.new | ||
@adapter = Flipper::Adapters::Dalli.new(memory_adapter, @cache) | ||
end | ||
|
||
def teardown | ||
@cache.flush | ||
end | ||
end |
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.
I also made these readers instead of accessors.