Class: ResilientCall::Storage::Redis

Inherits:
Base
  • Object
show all
Defined in:
lib/resilient_call/storage/redis.rb

Overview

Shares circuit state across processes through Redis. The Redis client is injected, so this class has no hard dependency on the redis gem itself — any object answering to get/set/del/keys works (including fakes in tests).

Concurrency note: read + write are separate round-trips, so two processes can race on the failure counter. The worst case is an occasional missed increment, never state corruption. Atomic Lua updates are a v0.3 concern.

Constant Summary collapse

KEY_PREFIX =
"resilient_call:circuit:"

Instance Method Summary collapse

Constructor Details

#initialize(redis_client) ⇒ Redis

Returns a new instance of Redis.



18
19
20
# File 'lib/resilient_call/storage/redis.rb', line 18

def initialize(redis_client)
  @redis = redis_client
end

Instance Method Details

#read(circuit_name) ⇒ Object



22
23
24
25
26
# File 'lib/resilient_call/storage/redis.rb', line 22

def read(circuit_name)
  raw = @redis.get(key_for(circuit_name))

  raw ? deserialize(raw) : nil
end

#reset(circuit_name) ⇒ Object



32
33
34
# File 'lib/resilient_call/storage/redis.rb', line 32

def reset(circuit_name)
  @redis.del(key_for(circuit_name))
end

#reset_allObject



36
37
38
39
# File 'lib/resilient_call/storage/redis.rb', line 36

def reset_all
  keys = @redis.keys("#{KEY_PREFIX}*")
  @redis.del(*keys) unless keys.empty?
end

#write(circuit_name, state) ⇒ Object



28
29
30
# File 'lib/resilient_call/storage/redis.rb', line 28

def write(circuit_name, state)
  @redis.set(key_for(circuit_name), serialize(state))
end