Class: Potlock::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/potlock/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key:) ⇒ Client

Returns a new instance of Client.



7
8
9
10
# File 'lib/potlock/client.rb', line 7

def initialize(key:)
  @key = key
  @lock_key = "#{key}_lock"
end

Instance Attribute Details

#keyObject

Returns the value of attribute key.



5
6
7
# File 'lib/potlock/client.rb', line 5

def key
  @key
end

#lock_keyObject

Returns the value of attribute lock_key.



5
6
7
# File 'lib/potlock/client.rb', line 5

def lock_key
  @lock_key
end

Instance Method Details

#fetchObject



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/potlock/client.rb', line 12

def fetch
  lock! do
    return redis.get(key) if redis.exists?(key)

    value = yield
    redis.set(key, value)
    value
  end
rescue Redlock::LockError => _e
  raise Potlock::LockError
end

#getObject



24
25
26
27
28
# File 'lib/potlock/client.rb', line 24

def get
  lock! { redis.get(key) }
rescue Redlock::LockError => _e
  raise Potlock::LockError
end

#set(&block) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/potlock/client.rb', line 30

def set(&block)
  value = lock!(&block)
  redis.set(key, value)
  value
rescue Redlock::LockError => _e
  raise Potlock::LockError
end