Class: RedisLocker::MethodLocker
- Includes:
- RedisConnection
- Defined in:
- lib/redis_locker/method_locker.rb
Constant Summary
Constants inherited from Locker
Instance Method Summary collapse
-
#initialize(model_locker, method_name) ⇒ MethodLocker
constructor
A new instance of MethodLocker.
- #lock ⇒ Object
- #lock! ⇒ Object
- #locked? ⇒ Boolean
- #unlock ⇒ Object
Methods inherited from Locker
Constructor Details
#initialize(model_locker, method_name) ⇒ MethodLocker
Returns a new instance of MethodLocker.
5 6 7 8 9 |
# File 'lib/redis_locker/method_locker.rb', line 5 def initialize(model_locker, method_name) @model_locker = model_locker @key_string = "LOCKER:#{model_locker.key_string}:#{method_name}" super end |
Instance Method Details
#lock ⇒ Object
11 12 13 14 15 16 |
# File 'lib/redis_locker/method_locker.rb', line 11 def lock return false if locked? @model_locker.lock redis.sadd(@key_string, @instance_hash) end |
#lock! ⇒ Object
18 19 20 21 22 23 |
# File 'lib/redis_locker/method_locker.rb', line 18 def lock! raise Errors::AlreadyLocked if locked? @model_locker.lock lock end |
#locked? ⇒ Boolean
25 26 27 |
# File 'lib/redis_locker/method_locker.rb', line 25 def locked? redis.scard(@key_string) > 1 end |
#unlock ⇒ Object
29 30 31 32 33 |
# File 'lib/redis_locker/method_locker.rb', line 29 def unlock return true unless locked? (redis.srem(@key_string, @instance_hash) && @model_locker.unlock) end |