class Flipper::Adapters::Redis
Attributes
Public Class Methods
Source
# File lib/flipper/adapters/redis.rb, line 27 def initialize(client, key_prefix: nil) @client = client @key_prefix = key_prefix @sadd_returns_boolean = with_connection do |conn| conn.class.respond_to?(:sadd_returns_boolean) && conn.class.sadd_returns_boolean end end
Public: Initializes a Redis
flipper adapter.
client - The Redis
client to use. key_prefix
- an optional prefix with which to namespace
flipper's Redis keys
Public Instance Methods
Source
# File lib/flipper/adapters/redis.rb, line 41 def add(feature) if redis_sadd_returns_boolean? with_connection { |conn| conn.sadd? features_key, feature.key } else with_connection { |conn| conn.sadd features_key, feature.key } end true end
Public: Adds a feature to the set of known features.
Source
# File lib/flipper/adapters/redis.rb, line 62 def clear(feature) with_connection { |conn| conn.del key_for(feature.key) } true end
Public: Clears the gate values for a feature.
Source
# File lib/flipper/adapters/redis.rb, line 117 def disable(feature, gate, thing) feature_key = key_for(feature.key) case gate.data_type when :boolean with_connection { |conn| conn.del feature_key } when :integer with_connection { |conn| conn.hset feature_key, gate.key, thing.value.to_s } when :set with_connection { |conn| conn.hdel feature_key, to_field(gate, thing) } when :json with_connection { |conn| conn.hdel feature_key, gate.key } else unsupported_data_type gate.data_type end true end
Public: Disables a gate for a given thing.
feature - The Flipper::Feature for the gate. gate - The Flipper::Gate to disable. thing - The Flipper::Type being disabled for the gate.
Returns true.
Source
# File lib/flipper/adapters/redis.rb, line 91 def enable(feature, gate, thing) feature_key = key_for(feature.key) case gate.data_type when :boolean clear(feature) with_connection { |conn| conn.hset feature_key, gate.key, thing.value.to_s } when :integer with_connection { |conn| conn.hset feature_key, gate.key, thing.value.to_s } when :set with_connection { |conn| conn.hset feature_key, to_field(gate, thing), 1 } when :json with_connection { |conn| conn.hset feature_key, gate.key, Typecast.to_json(thing.value) } else unsupported_data_type gate.data_type end true end
Public: Enables a gate for a given thing.
feature - The Flipper::Feature for the gate. gate - The Flipper::Gate to enable. thing - The Flipper::Type being enabled for the gate.
Returns true.
Source
# File lib/flipper/adapters/redis.rb, line 36 def features read_feature_keys end
Public: The set of known features.
Source
# File lib/flipper/adapters/redis.rb, line 14 def features_key "#{key_prefix}flipper_features" end
Source
# File lib/flipper/adapters/redis.rb, line 70 def get(feature) doc = doc_for(feature) result_for_feature(feature, doc) end
Public: Gets the values for all gates for a given feature.
Returns a Hash of Flipper::Gate#key => value.
Source
# File lib/flipper/adapters/redis.rb, line 79 def get_all features = read_feature_keys.map { |key| Flipper::Feature.new(key, self) } read_many_features(features) end
Source
# File lib/flipper/adapters/redis.rb, line 75 def get_multi(features) read_many_features(features) end
Source
# File lib/flipper/adapters/redis.rb, line 18 def key_for(feature_name) "#{key_prefix}#{feature_name}" end
Source
# File lib/flipper/adapters/redis.rb, line 51 def remove(feature) if redis_sadd_returns_boolean? with_connection { |conn| conn.srem? features_key, feature.key } else with_connection { |conn| conn.srem features_key, feature.key } end with_connection { |conn| conn.del key_for(feature.key) } true end
Public: Removes a feature from the set of known features.
Private Instance Methods
Source
# File lib/flipper/adapters/redis.rb, line 157 def doc_for(feature, pipeline: nil) if pipeline pipeline.hgetall(key_for(feature.key)) else with_connection { |conn| conn.hgetall(key_for(feature.key)) } end end
Private: Gets a hash of fields => values for the given feature.
Returns a Hash of fields => values.
Source
# File lib/flipper/adapters/redis.rb, line 165 def docs_for(features) with_connection do |conn| conn.pipelined do |pipeline| features.each do |feature| doc_for(feature, pipeline: pipeline) end end end end
Source
# File lib/flipper/adapters/redis.rb, line 205 def fields_to_gate_value(fields, gate) regex = %r{^#{Regexp.escape(gate.key.to_s)}/} keys = fields.grep(regex) values = keys.map { |key| key.split('/', 2).last } values.to_set end
Private: Returns a set of values given an array of fields and a gate.
Returns a Set of the values enabled for the gate.
Source
# File lib/flipper/adapters/redis.rb, line 150 def read_feature_keys with_connection { |conn| conn.smembers(features_key).to_set } end
Source
# File lib/flipper/adapters/redis.rb, line 141 def read_many_features(features) docs = docs_for(features) result = {} features.zip(docs) do |feature, doc| result[feature.key] = result_for_feature(feature, doc) end result end
Source
# File lib/flipper/adapters/redis.rb, line 137 def redis_sadd_returns_boolean? @sadd_returns_boolean end
Source
# File lib/flipper/adapters/redis.rb, line 175 def result_for_feature(feature, doc) result = {} fields = doc.keys feature.gates.each do |gate| result[gate.key] = case gate.data_type when :boolean, :integer doc[gate.key.to_s] when :set fields_to_gate_value fields, gate when :json value = doc[gate.key.to_s] Typecast.from_json(value) else unsupported_data_type gate.data_type end end result end
Source
# File lib/flipper/adapters/redis.rb, line 198 def to_field(gate, thing) "#{gate.key}/#{thing.value}" end
Private: Converts gate and thing to hash key.
Source
# File lib/flipper/adapters/redis.rb, line 213 def unsupported_data_type(data_type) raise "#{data_type} is not supported by this adapter" end
Private