diff --git a/lib/cover_rage/stores/redis.rb b/lib/cover_rage/stores/redis.rb index a3e233b..f7aeacb 100644 --- a/lib/cover_rage/stores/redis.rb +++ b/lib/cover_rage/stores/redis.rb @@ -9,13 +9,10 @@ module CoverRage module Stores class Redis KEY = 'cover_rage_records' + IS_REDIS_BELOW_V5 = Gem::Version.new(::Redis::VERSION) < Gem::Version.new('5') def initialize(url) - @redis = - if url.start_with?('rediss') - ::Redis.new(url:, ssl_params: { verify_mode: OpenSSL::SSL::VERIFY_NONE }) - else - ::Redis.new(url:) - end + @redis = new_redis(url) + @redis_for_below_v5 = new_redis(url) if IS_REDIS_BELOW_V5 end def transaction(&) @@ -42,7 +39,14 @@ def update(records) end def list - result = @redis.hgetall(KEY) + # For Redis versions below 5, we need to use the separate client to read + # the data while the transaction is in progress. + client = if Thread.current[:redis_multi] && IS_REDIS_BELOW_V5 + @redis_for_below_v5 + else + @redis + end + result = client.hgetall(KEY) return [] if result.empty? result.map { |_, value| Record.new(**JSON.parse(value)) } @@ -51,6 +55,16 @@ def list def clear @redis.del(KEY) end + + private + + def new_redis(url) + if url.start_with?('rediss') + ::Redis.new(url:, ssl_params: { verify_mode: OpenSSL::SSL::VERIFY_NONE }) + else + ::Redis.new(url:) + end + end end end end