Class: CoverRage::Stores::Redis
- Inherits:
-
Object
- Object
- CoverRage::Stores::Redis
- Defined in:
- lib/cover_rage/stores/redis.rb
Constant Summary collapse
- KEY =
'cover_rage_records'- IS_REDIS_BELOW_V5 =
Gem::Version.new(::Redis::VERSION) < Gem::Version.new('5')
Instance Method Summary collapse
- #clear ⇒ Object
-
#initialize(url) ⇒ Redis
constructor
A new instance of Redis.
- #list ⇒ Object
- #transaction ⇒ Object
- #update(records) ⇒ Object
Constructor Details
#initialize(url) ⇒ Redis
Returns a new instance of Redis.
13 14 15 16 |
# File 'lib/cover_rage/stores/redis.rb', line 13 def initialize(url) @redis = new_redis(url) @redis_for_below_v5 = new_redis(url) if IS_REDIS_BELOW_V5 end |
Instance Method Details
#clear ⇒ Object
55 56 57 |
# File 'lib/cover_rage/stores/redis.rb', line 55 def clear @redis.del(KEY) end |
#list ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/cover_rage/stores/redis.rb', line 41 def list # 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)) } end |
#transaction ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/cover_rage/stores/redis.rb', line 18 def transaction(&) loop do break if @redis.watch(KEY) do @redis.multi do |multi| Thread.current[:redis_multi] = multi yield ensure Thread.current[:redis_multi] = nil end end end end |
#update(records) ⇒ Object
31 32 33 34 35 36 37 38 39 |
# File 'lib/cover_rage/stores/redis.rb', line 31 def update(records) arguments = [] records.each do |record| arguments.push(record.path, JSON.dump(record.to_h)) end client = Thread.current[:redis_multi] || @redis client.hset(KEY, *arguments) end |