class FeatureFlagger::Storage::Redis

Constants

DEFAULT_NAMESPACE
RESOURCE_PREFIX
SCAN_EACH_BATCH_SIZE

Public Class Methods

default_client() click to toggle source
# File lib/feature_flagger/storage/redis.rb, line 16
def self.default_client
  redis = ::Redis.new(url: ENV['REDIS_URL'])
  ns = ::Redis::Namespace.new(DEFAULT_NAMESPACE, :redis => redis)
  new(ns)
end
new(redis) click to toggle source
# File lib/feature_flagger/storage/redis.rb, line 12
def initialize(redis)
  @redis = redis
end

Public Instance Methods

add(feature_key, resource_name, resource_id) click to toggle source
# File lib/feature_flagger/storage/redis.rb, line 33
def add(feature_key, resource_name, resource_id)
  resource_key = resource_key(resource_name, resource_id)

  @redis.multi do |redis|
    redis.sadd(feature_key, resource_id)
    redis.sadd(resource_key, feature_key)
  end
end
add_all(global_key, key) click to toggle source
# File lib/feature_flagger/storage/redis.rb, line 56
def add_all(global_key, key)
  @redis.sadd(global_key, key)
  remove_feature_key_from_resources(key)
end
all_values(key) click to toggle source
# File lib/feature_flagger/storage/redis.rb, line 61
def all_values(key)
  @redis.smembers(key)
end
feature_keys() click to toggle source
# File lib/feature_flagger/storage/redis.rb, line 71
def feature_keys
  feature_keys = []

  @redis.scan_each(match: "*") do |key|
    # Reject keys related to feature responsible for return
    # released features for a given account.
    next if key.start_with?("#{RESOURCE_PREFIX}:")

    feature_keys << key
  end

  feature_keys
end
fetch_releases(resource_name, resource_id, global_key) click to toggle source
# File lib/feature_flagger/storage/redis.rb, line 22
def fetch_releases(resource_name, resource_id, global_key)
  resource_key = resource_key(resource_name, resource_id)
  releases = @redis.sunion(resource_key, global_key)

  releases.select{ |release| release.start_with?(resource_name) }
end
has_value?(key, value) click to toggle source
# File lib/feature_flagger/storage/redis.rb, line 29
def has_value?(key, value)
  @redis.sismember(key, value)
end
remove(feature_key, resource_name, resource_id) click to toggle source
# File lib/feature_flagger/storage/redis.rb, line 42
def remove(feature_key, resource_name, resource_id)
  resource_key = resource_key(resource_name, resource_id)

  @redis.multi do |redis|
    redis.srem(feature_key, resource_id)
    redis.srem(resource_key, feature_key)
  end
end
remove_all(global_key, feature_key) click to toggle source
# File lib/feature_flagger/storage/redis.rb, line 51
def remove_all(global_key, feature_key)
  @redis.srem(global_key, feature_key)
  remove_feature_key_from_resources(feature_key)
end
search_keys(query) click to toggle source

DEPRECATED: this method will be removed from public api on v2.0 version. use instead the feature_keys method.

# File lib/feature_flagger/storage/redis.rb, line 67
def search_keys(query)
  @redis.scan_each(match: query)
end
synchronize_feature_and_resource() click to toggle source
# File lib/feature_flagger/storage/redis.rb, line 85
def synchronize_feature_and_resource
  FeatureFlagger::Storage::FeatureKeysMigration.new(
    @redis,
    FeatureFlagger.control,
  ).call
end

Private Instance Methods

remove_feature_key_from_resources(feature_key) click to toggle source
# File lib/feature_flagger/storage/redis.rb, line 102
def remove_feature_key_from_resources(feature_key)
  cursor = 0
  resource_name = feature_key.split(":").first

  loop do
    cursor, resource_ids = @redis.sscan(feature_key, cursor, count: SCAN_EACH_BATCH_SIZE)

    @redis.multi do |redis|
      resource_ids.each do |resource_id|
        key = resource_key(resource_name, resource_id)
        redis.srem(key, feature_key)
        redis.srem(feature_key, resource_id)
      end
    end

    break if cursor == "0"
  end
end
resource_key(resource_name, resource_id) click to toggle source
# File lib/feature_flagger/storage/redis.rb, line 94
def resource_key(resource_name, resource_id)
  FeatureFlagger::Storage::Keys.resource_key(
    RESOURCE_PREFIX,
    resource_name,
    resource_id,
  )
end