Class: Redlocker::Client
- Inherits:
-
Object
- Object
- Redlocker::Client
- Defined in:
- lib/redlocker/client.rb
Overview
The Redlocker::Client class allows to easily acquire and keep distributed
locks using redis. The acquired lock gets automatically renewed every
second from a thread, i.e. its 5 second expiry value gets renewed in redis
every second, and it gets released when the given block finishes.
Instance Attribute Summary collapse
-
#namespace ⇒ Object
readonly
Returns the value of attribute namespace.
-
#redis ⇒ Object
readonly
Returns the value of attribute redis.
Instance Method Summary collapse
-
#initialize(redis:, namespace: nil) ⇒ Client
constructor
Creates a new
Redlocker::Clientinstance. -
#with_lock(name, timeout:, delay: 0.25, &block) ⇒ Object
Acquires the specified lock or raises a
Redlocker::TimeoutErrorwhen the lock can not be acquired within the specifiedtimeout.
Constructor Details
#initialize(redis:, namespace: nil) ⇒ Client
Creates a new Redlocker::Client instance.
28 29 30 31 |
# File 'lib/redlocker/client.rb', line 28 def initialize(redis:, namespace: nil) @redis = redis @namespace = namespace end |
Instance Attribute Details
#namespace ⇒ Object (readonly)
Returns the value of attribute namespace.
17 18 19 |
# File 'lib/redlocker/client.rb', line 17 def namespace @namespace end |
#redis ⇒ Object (readonly)
Returns the value of attribute redis.
17 18 19 |
# File 'lib/redlocker/client.rb', line 17 def redis @redis end |
Instance Method Details
#with_lock(name, timeout:, delay: 0.25, &block) ⇒ Object
Acquires the specified lock or raises a Redlocker::TimeoutError when
the lock can not be acquired within the specified timeout. You can pass
a delay, which specifies how long to wait between subsequent checks of
whether or not the lock is free. When the lock has been successfully
acquired, it gets refreshed every second, i.e. its expiry value of 5
seconds is refreshed within redis every second.
55 56 57 |
# File 'lib/redlocker/client.rb', line 55 def with_lock(name, timeout:, delay: 0.25, &block) Lock.new(client: self, name: name, timeout: timeout, delay: delay).acquire(&block) end |