class RedisLocker::ModelLocker

Attributes

key_string[R]

Public Class Methods

new(model_instance) click to toggle source
Calls superclass method RedisLocker::Locker::new
# File lib/redis_locker/model_locker.rb, line 8
def initialize(model_instance)
  raise Errors::NotModel unless model_instance.respond_to?(:id)

  @key_string = "LOCKER:#{model_instance.class}:#{model_instance.id}"
  super
end

Public Instance Methods

lock() click to toggle source
# File lib/redis_locker/model_locker.rb, line 15
def lock
  return false if locked?

  redis.sadd(@key_string, @instance_hash)
end
lock!() click to toggle source
# File lib/redis_locker/model_locker.rb, line 21
def lock!
  raise Errors::AlreadyLocked if locked?

  lock
end
locked?() click to toggle source
# File lib/redis_locker/model_locker.rb, line 27
def locked?
  redis.scard(@key_string) > 1 # it has to have NULL_SET_VALUE, otherwise redis will free key
end
unlock() click to toggle source
# File lib/redis_locker/model_locker.rb, line 31
def unlock
  return true unless locked?

  redis.srem(@key_string, @instance_hash)
end