Class: Flipper::Adapters::Redis

Inherits:
Object
  • Object
show all
Includes:
Flipper::Adapter, RedisShared
Defined in:
lib/flipper/adapters/redis.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, key_prefix: nil) ⇒ Redis

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


27
28
29
30
31
32
33
# 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

Instance Attribute Details

#key_prefixObject (readonly)

Returns the value of attribute key_prefix.



12
13
14
# File 'lib/flipper/adapters/redis.rb', line 12

def key_prefix
  @key_prefix
end

Instance Method Details

#add(feature) ⇒ Object

Public: Adds a feature to the set of known features.



41
42
43
44
45
46
47
48
# 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

#clear(feature) ⇒ Object

Public: Clears the gate values for a feature.



62
63
64
65
# File 'lib/flipper/adapters/redis.rb', line 62

def clear(feature)
  with_connection { |conn| conn.del key_for(feature.key) }
  true
end

#disable(feature, gate, thing) ⇒ Object

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.



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# 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

#enable(feature, gate, thing) ⇒ Object

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.



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# 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

#featuresObject

Public: The set of known features.



36
37
38
# File 'lib/flipper/adapters/redis.rb', line 36

def features
  read_feature_keys
end

#features_keyObject



14
15
16
# File 'lib/flipper/adapters/redis.rb', line 14

def features_key
  "#{key_prefix}flipper_features"
end

#get(feature) ⇒ Object

Public: Gets the values for all gates for a given feature.

Returns a Hash of Flipper::Gate#key => value.



70
71
72
73
# File 'lib/flipper/adapters/redis.rb', line 70

def get(feature)
  doc = doc_for(feature)
  result_for_feature(feature, doc)
end

#get_allObject



79
80
81
82
# 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

#get_multi(features) ⇒ Object



75
76
77
# File 'lib/flipper/adapters/redis.rb', line 75

def get_multi(features)
  read_many_features(features)
end

#key_for(feature_name) ⇒ Object



18
19
20
# File 'lib/flipper/adapters/redis.rb', line 18

def key_for(feature_name)
  "#{key_prefix}#{feature_name}"
end

#remove(feature) ⇒ Object

Public: Removes a feature from the set of known features.



51
52
53
54
55
56
57
58
59
# 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