Sha256: e8c0a82aa599798a6c99ef114997e59bf69f4b01f57e4df9f20902a9b4a95197

Contents?: true

Size: 1.4 KB

Versions: 6

Compression:

Stored size: 1.4 KB

Contents

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

      protected

      def get(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(scope_key(scope), key, value)
      rescue ::Redis::BaseError => e
        raise Verdict::StorageError, "Redis error: #{e.message}"
      end

      def remove(scope, key)
        redis.hdel(scope_key(scope), key)
      rescue ::Redis::BaseError => e
        raise Verdict::StorageError, "Redis error: #{e.message}"
      end

      def clear(scope, options)
        scrub(scope)
        redis.del(scope_key(scope))
      rescue ::Redis::BaseError => e
        raise Verdict::StorageError, "Redis error: #{e.message}"
      end

      private

      def scope_key(scope)
        "#{@key_prefix}#{scope}"
      end

      def scrub(scope, cursor: 0)
        cursor, results = redis.hscan(scope_key(scope), cursor, count: PAGE_SIZE)
        results.map(&:first).each do |key|
          remove(scope, key)
        end
        scrub(scope, cursor: cursor) unless cursor.to_i.zero?
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
verdict-0.15.2 lib/verdict/storage/redis_storage.rb
verdict-0.15.1 lib/verdict/storage/redis_storage.rb
verdict-0.15.0 lib/verdict/storage/redis_storage.rb
verdict-0.14.0 lib/verdict/storage/redis_storage.rb
verdict-0.13.0 lib/verdict/storage/redis_storage.rb
verdict-0.12.0 lib/verdict/storage/redis_storage.rb