lib/verdict/storage/redis_storage.rb in verdict-0.9.0 vs lib/verdict/storage/redis_storage.rb in verdict-0.10.0
- old
+ new
@@ -1,36 +1,53 @@
module Verdict
module Storage
class RedisStorage < BaseStorage
+ PAGE_SIZE = 50
+
attr_accessor :redis, :key_prefix
def initialize(redis = nil, options = {})
@redis = redis
@key_prefix = options[:key_prefix] || 'experiments/'
end
def get(scope, key)
- redis.hget("#{@key_prefix}#{scope}", key)
+ redis.hget(scope_key(scope), key)
rescue ::Redis::BaseError => e
raise Verdict::StorageError, "Redis error: #{e.message}"
end
def set(scope, key, value)
- redis.hset("#{@key_prefix}#{scope}", key, value)
+ redis.hset(scope_key(scope), key, value)
rescue ::Redis::BaseError => e
raise Verdict::StorageError, "Redis error: #{e.message}"
end
def remove(scope, key)
- redis.hdel("#{@key_prefix}#{scope}", key)
+ redis.hdel(scope_key(scope), key)
rescue ::Redis::BaseError => e
raise Verdict::StorageError, "Redis error: #{e.message}"
end
+ def cleanup(scope)
+ clear(scope)
+ redis.del(scope_key(scope))
+ rescue ::Redis::BaseError => e
+ raise Verdict::StorageError, "Redis error: #{e.message}"
+ end
+
private
- def generate_scope_key(scope)
+ def scope_key(scope)
"#{@key_prefix}#{scope}"
+ end
+
+ def clear(scope, cursor: 0)
+ cursor, results = redis.hscan(scope_key(scope), cursor, count: PAGE_SIZE)
+ results.map(&:first).each do |key|
+ remove(scope, key)
+ end
+ clear(scope, cursor: cursor) unless cursor.to_i.zero?
end
end
end
end