Class: RedisLocker::MethodLocker

Inherits:
Locker
  • Object
show all
Includes:
RedisConnection
Defined in:
lib/redis_locker/method_locker.rb

Constant Summary

Constants inherited from Locker

Locker::NULL_SET_VALUE

Instance Method Summary collapse

Methods inherited from Locker

#with_redis_lock

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

#lockObject



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

Returns:

  • (Boolean)


25
26
27
# File 'lib/redis_locker/method_locker.rb', line 25

def locked?
  redis.scard(@key_string) > 1
end

#unlockObject



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