Class: Flipper::Adapters::Redis

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Flipper::Adapter

#default_config, #export, #import, included, #name, #read_only?

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


25
26
27
28
# File 'lib/flipper/adapters/redis.rb', line 25

def initialize(client, key_prefix: nil)
  @client = client
  @key_prefix = key_prefix
end

Instance Attribute Details

#key_prefixObject (readonly)

Returns the value of attribute key_prefix.



10
11
12
# File 'lib/flipper/adapters/redis.rb', line 10

def key_prefix
  @key_prefix
end

Instance Method Details

#add(feature) ⇒ Object

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



36
37
38
39
40
41
42
43
# File 'lib/flipper/adapters/redis.rb', line 36

def add(feature)
  if redis_sadd_returns_boolean?
    @client.sadd? features_key, feature.key
  else
    @client.sadd features_key, feature.key
  end
  true
end

#clear(feature) ⇒ Object

Public: Clears the gate values for a feature.



57
58
59
60
# File 'lib/flipper/adapters/redis.rb', line 57

def clear(feature)
  @client.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.



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/flipper/adapters/redis.rb', line 112

def disable(feature, gate, thing)
  feature_key = key_for(feature.key)
  case gate.data_type
  when :boolean
    @client.del feature_key
  when :integer
    @client.hset feature_key, gate.key, thing.value.to_s
  when :set
    @client.hdel feature_key, to_field(gate, thing)
  when :json
    @client.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.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/flipper/adapters/redis.rb', line 86

def enable(feature, gate, thing)
  feature_key = key_for(feature.key)
  case gate.data_type
  when :boolean
    clear(feature)
    @client.hset feature_key, gate.key, thing.value.to_s
  when :integer
    @client.hset feature_key, gate.key, thing.value.to_s
  when :set
    @client.hset feature_key, to_field(gate, thing), 1
  when :json
    @client.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.



31
32
33
# File 'lib/flipper/adapters/redis.rb', line 31

def features
  read_feature_keys
end

#features_keyObject



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

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.



65
66
67
68
# File 'lib/flipper/adapters/redis.rb', line 65

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

#get_allObject



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

def get_all
  features = read_feature_keys.map { |key| Flipper::Feature.new(key, self) }
  read_many_features(features)
end

#get_multi(features) ⇒ Object



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

def get_multi(features)
  read_many_features(features)
end

#key_for(feature_name) ⇒ Object



16
17
18
# File 'lib/flipper/adapters/redis.rb', line 16

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

#remove(feature) ⇒ Object

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



46
47
48
49
50
51
52
53
54
# File 'lib/flipper/adapters/redis.rb', line 46

def remove(feature)
  if redis_sadd_returns_boolean?
    @client.srem? features_key, feature.key
  else
    @client.srem features_key, feature.key
  end
  @client.del key_for(feature.key)
  true
end