Class: Rollout::Adapters::Redis
- Inherits:
-
Object
- Object
- Rollout::Adapters::Redis
- Defined in:
- lib/rollout/adapters/redis.rb
Constant Summary collapse
- FEATURES_KEY =
'feature:__features__'
Instance Method Summary collapse
- #clear_features ⇒ Object
- #delete_feature(name) ⇒ Object
- #delete_feature_events(name) ⇒ Object
- #feature_events(name, limit: nil) ⇒ Object
- #feature_exists?(name) ⇒ Boolean
- #feature_names ⇒ Object
- #feature_updated_at(name) ⇒ Object
- #fetch_feature(name) ⇒ Object
- #fetch_features(names) ⇒ Object
- #global_events(limit: nil) ⇒ Object
-
#initialize(client) ⇒ Redis
constructor
A new instance of Redis.
- #mutate_feature(name) ⇒ Object
- #record_event(event, history_length:, global: false) ⇒ Object
- #save_feature(state) ⇒ Object
Constructor Details
#initialize(client) ⇒ Redis
Returns a new instance of Redis.
13 14 15 |
# File 'lib/rollout/adapters/redis.rb', line 13 def initialize(client) @client = client end |
Instance Method Details
#clear_features ⇒ Object
53 54 55 |
# File 'lib/rollout/adapters/redis.rb', line 53 def clear_features @client.del(FEATURES_KEY) end |
#delete_feature(name) ⇒ Object
46 47 48 49 50 51 |
# File 'lib/rollout/adapters/redis.rb', line 46 def delete_feature(name) names = feature_names names.delete(name.to_s) @client.set(FEATURES_KEY, names.join(',')) @client.del(key(name)) end |
#delete_feature_events(name) ⇒ Object
95 96 97 |
# File 'lib/rollout/adapters/redis.rb', line 95 def delete_feature_events(name) @client.del(events_key(name)) end |
#feature_events(name, limit: nil) ⇒ Object
82 83 84 |
# File 'lib/rollout/adapters/redis.rb', line 82 def feature_events(name, limit: nil) events_from(events_key(name), limit: limit) end |
#feature_exists?(name) ⇒ Boolean
32 33 34 35 36 37 38 |
# File 'lib/rollout/adapters/redis.rb', line 32 def feature_exists?(name) if @client.respond_to?(:exists?) @client.exists?(key(name)) else @client.exists(key(name)) end end |
#feature_names ⇒ Object
28 29 30 |
# File 'lib/rollout/adapters/redis.rb', line 28 def feature_names (@client.get(FEATURES_KEY) || '').split(',') end |
#feature_updated_at(name) ⇒ Object
90 91 92 93 |
# File 'lib/rollout/adapters/redis.rb', line 90 def feature_updated_at(name) _, score = @client.zrange(events_key(name), 0, 0, with_scores: true).first Time.at(-score.to_f / 1_000_000) if score end |
#fetch_feature(name) ⇒ Object
17 18 19 |
# File 'lib/rollout/adapters/redis.rb', line 17 def fetch_feature(name) ::Rollout::Redis::Codec.decode(name, @client.get(key(name))) end |
#fetch_features(names) ⇒ Object
21 22 23 24 25 26 |
# File 'lib/rollout/adapters/redis.rb', line 21 def fetch_features(names) return [] if names.empty? payloads = @client.mget(*names.map { |name| key(name) }) names.zip(payloads).map { |name, payload| ::Rollout::Redis::Codec.decode(name, payload) } end |
#global_events(limit: nil) ⇒ Object
86 87 88 |
# File 'lib/rollout/adapters/redis.rb', line 86 def global_events(limit: nil) events_from(global_events_key, limit: limit) end |
#mutate_feature(name) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/rollout/adapters/redis.rb', line 57 def mutate_feature(name) mutation = yield fetch_feature(name) save_feature(mutation.fetch(:state)) event = mutation[:event] if event record_event( event, history_length: mutation.fetch(:history_length), global: mutation[:global], ) end mutation end |
#record_event(event, history_length:, global: false) ⇒ Object
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/rollout/adapters/redis.rb', line 71 def record_event(event, history_length:, global: false) storage_key = events_key(event.feature) @client.zadd(storage_key, -event., event.serialize) @client.zremrangebyrank(storage_key, history_length, -1) return unless global @client.zadd(global_events_key, -event., event.serialize) @client.zremrangebyrank(global_events_key, history_length, -1) end |
#save_feature(state) ⇒ Object
40 41 42 43 44 |
# File 'lib/rollout/adapters/redis.rb', line 40 def save_feature(state) @client.set(key(state.name), ::Rollout::Redis::Codec.encode(state)) names = feature_names.map(&:to_s) | [state.name.to_s] @client.set(FEATURES_KEY, names.join(',')) end |