Skip to content
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

Merged
merged 2 commits into from Jun 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Expand Up @@ -9,6 +9,7 @@ script: bundle exec rake
services:
- redis-server
- mongodb
- memcached
env:
- RAILS_VERSION=5.0.0.beta3
- RAILS_VERSION=4.2.5
Expand Down
1 change: 1 addition & 0 deletions Gemfile
Expand Up @@ -15,6 +15,7 @@ gem 'rack-test', '~> 0.6.3'
gem 'sqlite3', '~> 1.3.11'
gem 'rails', "~> #{ENV["RAILS_VERSION"] || '4.2.5'}"
gem 'minitest', '~> 5.8.0'
gem 'dalli'

# for active support tests in test/ and only needed for ruby 2.2.x
gem 'test-unit', '~> 3.0'
Expand Down
75 changes: 75 additions & 0 deletions lib/flipper/adapters/dalli.rb
@@ -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
Copy link
Collaborator

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.


#Public: The name of the adapter.
attr_accessor :name

# Public
def initialize(adapter, cache, ttl = 0)
@adapter = adapter
@name = adapter.name
Copy link
Collaborator

Choose a reason for hiding this comment

The 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.

Copy link
Collaborator

Choose a reason for hiding this comment

The 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
17 changes: 17 additions & 0 deletions test/adapters/dalli_test.rb
@@ -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