Skip to content

Commit

Permalink
Use more-efficient Redis sismember command to check if feature exists
Browse files Browse the repository at this point in the history
  • Loading branch information
owst committed Oct 5, 2023
1 parent d06da06 commit f2fae5b
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 1 deletion.
7 changes: 7 additions & 0 deletions lib/flipper/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ def get_multi(features)
result
end

# Public: Does a feature with this key exist.
#
# Returns true if a feature exists with this key, else false.
def exist?(key)
features.include?(key)
end

# Public: Ensure that adapter is in sync with source adapter provided.
#
# source - The source dsl, adapter or export to import.
Expand Down
5 changes: 5 additions & 0 deletions lib/flipper/adapters/redis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ def features
read_feature_keys
end

# Public: Does a feature with this key exist.
def exist?(key)
@client.sismember(features_key, key)
end

# Public: Adds a feature to the set of known features.
def add(feature)
if redis_sadd_returns_boolean?
Expand Down
2 changes: 1 addition & 1 deletion lib/flipper/feature.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def add
#
# Returns true if exists in adapter else false.
def exist?
instrument(:exist?) { adapter.features.include?(key) }
instrument(:exist?) { adapter.exist?(key) }
end

# Public: Removes this feature.
Expand Down
6 changes: 6 additions & 0 deletions spec/flipper/adapters/redis_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@

it_should_behave_like 'a flipper adapter'

it 'correctly identifies if a with feature with the given name exists' do
expect {
subject.add(Flipper::Feature.new(:foo, subject))
}.to change { subject.exist?(:foo) }.from(false).to(true)
end

it 'configures itself on load' do
Flipper.configuration = nil
Flipper.instance = nil
Expand Down

0 comments on commit f2fae5b

Please sign in to comment.