Class: Flipper::Adapters::RedisCache
- Inherits:
-
Object
- Object
- Flipper::Adapters::RedisCache
- Includes:
- Flipper::Adapter
- Defined in:
- lib/flipper/adapters/redis_cache.rb
Overview
Public: Adapter that wraps another adapter with the ability to cache adapter calls in Redis
Constant Summary collapse
- Version =
'v1'.freeze
- Namespace =
"flipper/#{Version}".freeze
- FeaturesKey =
"#{Namespace}/features".freeze
- GetAllKey =
"#{Namespace}/get_all".freeze
Instance Attribute Summary collapse
-
#cache ⇒ Object
readonly
Internal.
-
#name ⇒ Object
readonly
Public: The name of the adapter.
Class Method Summary collapse
-
.key_for(key) ⇒ Object
Private.
Instance Method Summary collapse
-
#add(feature) ⇒ Object
Public.
-
#clear(feature) ⇒ Object
Public.
-
#disable(feature, gate, thing) ⇒ Object
Public.
-
#enable(feature, gate, thing) ⇒ Object
Public.
-
#features ⇒ Object
Public.
-
#get(feature) ⇒ Object
Public.
- #get_all ⇒ Object
- #get_multi(features) ⇒ Object
-
#initialize(adapter, cache, ttl = 3600) ⇒ RedisCache
constructor
Public.
-
#remove(feature) ⇒ Object
Public.
Constructor Details
#initialize(adapter, cache, ttl = 3600) ⇒ RedisCache
Public
27 28 29 30 31 32 |
# File 'lib/flipper/adapters/redis_cache.rb', line 27 def initialize(adapter, cache, ttl = 3600) @adapter = adapter @name = :redis_cache @cache = cache @ttl = ttl end |
Instance Attribute Details
#cache ⇒ Object (readonly)
Internal
21 22 23 |
# File 'lib/flipper/adapters/redis_cache.rb', line 21 def cache @cache end |
#name ⇒ Object (readonly)
Public: The name of the adapter.
24 25 26 |
# File 'lib/flipper/adapters/redis_cache.rb', line 24 def name @name end |
Class Method Details
.key_for(key) ⇒ Object
Private
16 17 18 |
# File 'lib/flipper/adapters/redis_cache.rb', line 16 def self.key_for(key) "#{Namespace}/feature/#{key}" end |
Instance Method Details
#add(feature) ⇒ Object
Public
40 41 42 43 44 |
# File 'lib/flipper/adapters/redis_cache.rb', line 40 def add(feature) result = @adapter.add(feature) @cache.del(FeaturesKey) result end |
#clear(feature) ⇒ Object
Public
55 56 57 58 59 |
# File 'lib/flipper/adapters/redis_cache.rb', line 55 def clear(feature) result = @adapter.clear(feature) @cache.del(key_for(feature.key)) result end |
#disable(feature, gate, thing) ⇒ Object
Public
95 96 97 98 99 |
# File 'lib/flipper/adapters/redis_cache.rb', line 95 def disable(feature, gate, thing) result = @adapter.disable(feature, gate, thing) @cache.del(key_for(feature.key)) result end |
#enable(feature, gate, thing) ⇒ Object
Public
88 89 90 91 92 |
# File 'lib/flipper/adapters/redis_cache.rb', line 88 def enable(feature, gate, thing) result = @adapter.enable(feature, gate, thing) @cache.del(key_for(feature.key)) result end |
#features ⇒ Object
Public
35 36 37 |
# File 'lib/flipper/adapters/redis_cache.rb', line 35 def features read_feature_keys end |
#get(feature) ⇒ Object
Public
62 63 64 65 66 |
# File 'lib/flipper/adapters/redis_cache.rb', line 62 def get(feature) fetch(key_for(feature.key)) do @adapter.get(feature) end end |
#get_all ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/flipper/adapters/redis_cache.rb', line 72 def get_all if @cache.setnx(GetAllKey, Time.now.to_i) @cache.expire(GetAllKey, @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 response else features = read_feature_keys.map { |key| Flipper::Feature.new(key, self) } read_many_features(features) end end |
#get_multi(features) ⇒ Object
68 69 70 |
# File 'lib/flipper/adapters/redis_cache.rb', line 68 def get_multi(features) read_many_features(features) end |
#remove(feature) ⇒ Object
Public
47 48 49 50 51 52 |
# File 'lib/flipper/adapters/redis_cache.rb', line 47 def remove(feature) result = @adapter.remove(feature) @cache.del(FeaturesKey) @cache.del(key_for(feature.key)) result end |