Skip to content

Commit

Permalink
Thread safety for memoizable.
Browse files Browse the repository at this point in the history
  • Loading branch information
jnunemaker committed Feb 15, 2013
1 parent e8eec56 commit a1c3804
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 22 deletions.
56 changes: 36 additions & 20 deletions lib/flipper/adapters/memoizable.rb
Expand Up @@ -5,32 +5,31 @@ module Adapters
class Memoizable < Decorator
FeaturesKey = :flipper_features

# Private: The cache of memoized adapter operations and results.
attr_reader :cache
# Internal
def self.cache
Thread.current[:flipper_memoize_cache] ||= {}
end

# Public
def initialize(adapter, cache = nil)
super(adapter)
@cache = cache || {}
# Internal
def self.memoizing?
!!Thread.current[:flipper_memoize]
end

# Public: Turns local caching on/off.
#
# value - The Boolean that decides if local caching is on.
def memoize=(value)
# Internal
def self.memoize=(value)
cache.clear
@memoize = value
Thread.current[:flipper_memoize] = value
end

# Public: Returns true for using local cache, false for not.
def memoizing?
!!@memoize
# Public
def initialize(adapter)
super(adapter)
end

# Public
def get(feature)
if memoizing?
@cache.fetch(feature) { @cache[feature] = super }
cache.fetch(feature) { cache[feature] = super }
else
super
end
Expand All @@ -39,22 +38,22 @@ def get(feature)
# Public
def enable(feature, gate, thing)
result = super
@cache.delete(feature) if memoizing?
cache.delete(feature) if memoizing?
result
end

# Public
def disable(feature, gate, thing)
result = super
@cache.delete(feature) if memoizing?
cache.delete(feature) if memoizing?
result
end

# Public
def features
if memoizing?
@cache.fetch(FeaturesKey) {
@cache[FeaturesKey] = super
cache.fetch(FeaturesKey) {
cache[FeaturesKey] = super
}
else
super
Expand All @@ -64,9 +63,26 @@ def features
# Public
def add(feature)
result = super
@cache.delete(FeaturesKey) if memoizing?
cache.delete(FeaturesKey) if memoizing?
result
end

# Internal
def cache
self.class.cache
end

# Internal: Turns local caching on/off.
#
# value - The Boolean that decides if local caching is on.
def memoize=(value)
self.class.memoize = value
end

# Internal: Returns true for using local cache, false for not.
def memoizing?
self.class.memoizing?
end
end
end
end
8 changes: 6 additions & 2 deletions spec/flipper/adapters/memoizable_spec.rb
Expand Up @@ -6,11 +6,15 @@
describe Flipper::Adapters::Memoizable do
let(:features_key) { described_class::FeaturesKey }

let(:cache) { {} }
let(:adapter) { Flipper::Adapters::Memory.new }
let(:flipper) { Flipper.new(adapter) }
let(:cache) { Thread.current[:flipper_memoize_cache] }

subject { described_class.new(adapter, cache) }
after do
described_class.memoize = nil
end

subject { described_class.new(adapter) }

it_should_behave_like 'a flipper adapter'

Expand Down
4 changes: 4 additions & 0 deletions spec/flipper/middleware/local_cache_spec.rb
Expand Up @@ -30,6 +30,10 @@
end.to_app
}

after do
flipper.adapter.memoize = nil
end

it "delegates" do
called = false
app = lambda { |env|
Expand Down

0 comments on commit a1c3804

Please sign in to comment.