lib/flipper/adapters/redis_cache.rb in flipper-redis-1.0.0 vs lib/flipper/adapters/redis_cache.rb in flipper-redis-1.1.0
- old
+ new
@@ -6,50 +6,41 @@
# Public: Adapter that wraps another adapter with the ability to cache
# adapter calls in Redis
class RedisCache
include ::Flipper::Adapter
- Version = 'v1'.freeze
- Namespace = "flipper/#{Version}".freeze
- FeaturesKey = "#{Namespace}/features".freeze
- GetAllKey = "#{Namespace}/get_all".freeze
-
- # Private
- def self.key_for(key)
- "#{Namespace}/feature/#{key}"
- end
-
# Internal
attr_reader :cache
- # Public: The name of the adapter.
- attr_reader :name
-
# Public
def initialize(adapter, cache, ttl = 3600)
@adapter = adapter
- @name = :redis_cache
@cache = cache
@ttl = ttl
+
+ @version = 'v1'.freeze
+ @namespace = "flipper/#{@version}".freeze
+ @features_key = "#{@namespace}/features".freeze
+ @get_all_key = "#{@namespace}/get_all".freeze
end
# Public
def features
read_feature_keys
end
# Public
def add(feature)
result = @adapter.add(feature)
- @cache.del(FeaturesKey)
+ @cache.del(@features_key)
result
end
# Public
def remove(feature)
result = @adapter.remove(feature)
- @cache.del(FeaturesKey)
+ @cache.del(@features_key)
@cache.del(key_for(feature.key))
result
end
# Public
@@ -69,17 +60,17 @@
def get_multi(features)
read_many_features(features)
end
def get_all
- if @cache.setnx(GetAllKey, Time.now.to_i)
- @cache.expire(GetAllKey, @ttl)
+ if @cache.setnx(@get_all_key, Time.now.to_i)
+ @cache.expire(@get_all_key, @ttl)
response = @adapter.get_all
response.each do |key, value|
set_with_ttl key_for(key), value
end
- set_with_ttl FeaturesKey, response.keys.to_set
+ set_with_ttl @features_key, response.keys.to_set
response
else
features = read_feature_keys.map { |key| Flipper::Feature.new(key, self) }
read_many_features(features)
end
@@ -100,14 +91,14 @@
end
private
def key_for(key)
- self.class.key_for(key)
+ "#{@namespace}/feature/#{key}"
end
def read_feature_keys
- fetch(FeaturesKey) { @adapter.features }
+ fetch(@features_key) { @adapter.features }
end
def read_many_features(features)
keys = features.map(&:key)
cache_result = Hash[keys.zip(multi_cache_get(keys))]