Skip to content

Commit

Permalink
Accept expires_in CacheStore contructor over options hash
Browse files Browse the repository at this point in the history
* Before we accepted all options passed them onto the cache methods, but
this allows us to be more specific

* use a write_options hash since we don't want to override the default
expires_in if its already been set on the cache instance
  • Loading branch information
AlexWheeler authored and Alex Wheeler committed Jun 9, 2017
1 parent 522b60a commit 17ffbec
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions lib/flipper/adapters/cache_store.rb
Expand Up @@ -22,45 +22,46 @@ def self.key_for(key)
attr_reader :name

# Public
def initialize(adapter, cache, options = nil)
def initialize(adapter, cache, expires_in: nil)
@adapter = adapter
@name = :cache_store
@cache = cache
@options = options
@write_options = {}
@write_options.merge!(expires_in: expires_in) if expires_in
end

# Public
def features
@cache.fetch(FeaturesKey, @options) do
@cache.fetch(FeaturesKey, @write_options) do
@adapter.features
end
end

# Public
def add(feature)
result = @adapter.add(feature)
@cache.delete(FeaturesKey, @options)
@cache.delete(FeaturesKey)
result
end

## Public
def remove(feature)
result = @adapter.remove(feature)
@cache.delete(FeaturesKey, @options)
@cache.delete(key_for(feature.key), @options)
@cache.delete(FeaturesKey)
@cache.delete(key_for(feature.key))
result
end

## Public
def clear(feature)
result = @adapter.clear(feature)
@cache.delete(key_for(feature.key), @options)
@cache.delete(key_for(feature.key))
result
end

## Public
def get(feature)
@cache.fetch(key_for(feature.key), @options) do
@cache.fetch(key_for(feature.key), @write_options) do
@adapter.get(feature)
end
end
Expand All @@ -72,7 +73,7 @@ def get_multi(features)
if uncached_features.any?
response = @adapter.get_multi(uncached_features)
response.each do |key, value|
@cache.write(key_for(key), value, @options)
@cache.write(key_for(key), value, @write_options)
result[key] = value
end
end
Expand All @@ -82,14 +83,14 @@ def get_multi(features)
## Public
def enable(feature, gate, thing)
result = @adapter.enable(feature, gate, thing)
@cache.delete(key_for(feature.key), @options)
@cache.delete(key_for(feature.key))
result
end

## Public
def disable(feature, gate, thing)
result = @adapter.disable(feature, gate, thing)
@cache.delete(key_for(feature.key), @options)
@cache.delete(key_for(feature.key))
result
end

Expand Down

0 comments on commit 17ffbec

Please sign in to comment.