Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added flag to memoized adapter to turn it on/off.

  • Loading branch information...
commit 4a03442160eb7e9311a628b5a902801ed13c890f 1 parent a891f05
@jnunemaker authored
Showing with 168 additions and 35 deletions.
  1. +29 −10 lib/flipper/adapters/memoized.rb
  2. +139 −25 spec/flipper/adapters/memoized_spec.rb
View
39 lib/flipper/adapters/memoized.rb
@@ -5,7 +5,7 @@ module Adapters
class Memoized < Decorator
FeaturesKey = :flipper_features
- # Private: The cache of adapter operations and results.
+ # Private: The cache of memoized adapter operations and results.
attr_reader :cache
# Public
@@ -14,36 +14,55 @@ def initialize(adapter, cache = {})
@cache = cache
end
+ # Public: Turns local caching on/off.
+ #
+ # value - The Boolean that decides if local caching is on.
+ def memoize=(value)
+ cache.clear
+ @memoize = value
+ end
+
+ # Public: Returns true for using local cache, false for not.
+ def memoizing?
+ !!@memoize
+ end
+
# Public
def get(feature)
- @cache.fetch(feature) {
- @cache[feature] = super
- }
+ if memoizing?
+ @cache.fetch(feature) { @cache[feature] = super }
+ else
+ super
+ end
end
# Public
def enable(feature, gate, thing)
result = super
- @cache.delete(feature)
+ @cache.delete(feature) if memoizing?
result
end
# Public
def disable(feature, gate, thing)
result = super
- @cache.delete(feature)
+ @cache.delete(feature) if memoizing?
result
end
def features
- @cache.fetch(FeaturesKey) {
- @cache[FeaturesKey] = super
- }
+ if memoizing?
+ @cache.fetch(FeaturesKey) {
+ @cache[FeaturesKey] = super
+ }
+ else
+ super
+ end
end
def add(feature)
result = super
- @cache.delete(FeaturesKey)
+ @cache.delete(FeaturesKey) if memoizing?
result
end
end
View
164 spec/flipper/adapters/memoized_spec.rb
@@ -4,6 +4,8 @@
require 'flipper/spec/shared_adapter_specs'
describe Flipper::Adapters::Memoized do
+ let(:features_key) { described_class::FeaturesKey }
+
let(:cache) { {} }
let(:adapter) { Flipper::Adapters::Memory.new }
let(:flipper) { Flipper.new(adapter) }
@@ -12,55 +14,167 @@
it_should_behave_like 'a flipper adapter'
+ describe "#memoize=" do
+ it "sets value" do
+ subject.memoize = true
+ subject.memoizing?.should be_true
+
+ subject.memoize = false
+ subject.memoizing?.should be_false
+ end
+
+ it "clears the local cache" do
+ subject.cache['some'] = 'thing'
+ subject.memoize = true
+ subject.cache.should be_empty
+ end
+ end
+
+ describe "#memoizing?" do
+ it "returns true if enabled" do
+ subject.memoize = true
+ subject.memoizing?.should be_true
+ end
+
+ it "returns false if disabled" do
+ subject.memoize = false
+ subject.memoizing?.should be_false
+ end
+ end
+
describe "#get" do
- before do
- @feature = flipper[:stats]
- @result = subject.get(@feature)
+ context "with memoization enabled" do
+ before do
+ subject.memoize = true
+ end
+
+ it "memoizes feature" do
+ feature = flipper[:stats]
+ result = subject.get(feature)
+ cache[feature].should be(result)
+ end
end
- it "memoizes feature" do
- cache[@feature].should be(@result)
+ context "with memoization disabled" do
+ before do
+ subject.memoize = false
+ end
+
+ it "returns result" do
+ feature = flipper[:stats]
+ result = subject.get(feature)
+ adapter_result = adapter.get(feature)
+ result.should eq(adapter_result)
+ end
end
end
describe "#enable" do
- before do
- @feature = flipper[:stats]
- gate = @feature.gate(:boolean)
+ context "with memoization enabled" do
+ before do
+ subject.memoize = true
+ end
- cache[@feature] = {:some => 'thing'}
- subject.enable(@feature, gate, Flipper::Types::Boolean.new)
+ it "unmemoizes feature" do
+ feature = flipper[:stats]
+ gate = feature.gate(:boolean)
+ cache[feature] = {:some => 'thing'}
+ subject.enable(feature, gate, flipper.bool)
+ cache[feature].should be_nil
+ end
end
- it "unmemoizes feature in cache" do
- cache[@feature].should be_nil
+ context "with memoization disabled" do
+ before do
+ subject.memoize = false
+ end
+
+ it "returns result" do
+ feature = flipper[:stats]
+ gate = feature.gate(:boolean)
+ result = subject.enable(feature, gate, flipper.bool)
+ adapter_result = adapter.enable(feature, gate, flipper.bool)
+ result.should eq(adapter_result)
+ end
end
end
describe "#disable" do
- before do
- @feature = flipper[:stats]
- gate = @feature.gate(:boolean)
+ context "with memoization enabled" do
+ before do
+ subject.memoize = true
+ end
- cache[@feature] = {:some => 'thing'}
- subject.disable(@feature, gate, Flipper::Types::Boolean.new)
+ it "unmemoizes feature" do
+ feature = flipper[:stats]
+ gate = feature.gate(:boolean)
+ cache[feature] = {:some => 'thing'}
+ subject.disable(feature, gate, flipper.bool)
+ cache[feature].should be_nil
+ end
end
- it "unmemoizes feature in cache" do
- cache[@feature].should be_nil
+ context "with memoization disabled" do
+ before do
+ subject.memoize = false
+ end
+
+ it "returns result" do
+ feature = flipper[:stats]
+ gate = feature.gate(:boolean)
+ result = subject.disable(feature, gate, flipper.bool)
+ adapter_result = adapter.disable(feature, gate, flipper.bool)
+ result.should eq(adapter_result)
+ end
end
end
describe "#features" do
- before do
- flipper[:stats].enable
- flipper[:search].disable
+ context "with memoization enabled" do
+ before do
+ subject.memoize = true
+ end
+
+ it "memoizes features" do
+ flipper[:stats].enable
+ flipper[:search].disable
+ result = subject.features
+ cache[:flipper_features].should be(result)
+ end
+ end
- @result = subject.features
+ context "with memoization disabled" do
+ before do
+ subject.memoize = false
+ end
+
+ it "returns result" do
+ subject.features.should eq(adapter.features)
+ end
+ end
+ end
+
+ describe "#add" do
+ context "with memoization enabled" do
+ before do
+ subject.memoize = true
+ end
+
+ it "unmemoizes features" do
+ cache[features_key] = {:some => 'thing'}
+ subject.add(flipper[:stats])
+ cache.should be_empty
+ end
end
- it "memoizes features" do
- cache[:flipper_features].should be(@result)
+ context "with memoization disabled" do
+ before do
+ subject.memoize = false
+ end
+
+ it "returns result" do
+ subject.add(flipper[:stats]).should eq(adapter.add(flipper[:stats]))
+ end
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.