Module: ActsAsLockableBy::Lockable::InstanceMethods

Defined in:
lib/acts_as_lockable_by/lockable.rb

Instance Method Summary collapse

Instance Method Details

#lock(locked_by_id) ⇒ Object



39
40
41
42
# File 'lib/acts_as_lockable_by/lockable.rb', line 39

def lock(locked_by_id)
  # Set if not exist
  redis.set(lock_key, locked_by_id, ex: ttl, nx: true)
end

#lock!(locked_by_id) ⇒ Object



44
45
46
# File 'lib/acts_as_lockable_by/lockable.rb', line 44

def lock!(locked_by_id)
  lock(locked_by_id) || raise(LockError)
end

#locked?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/acts_as_lockable_by/lockable.rb', line 52

def locked?
  locked_by_id.present?
end

#locked_by_idObject



48
49
50
# File 'lib/acts_as_lockable_by/lockable.rb', line 48

def locked_by_id
  redis.get(lock_key)
end

#renew_lock(locked_by_id) ⇒ Object



68
69
70
71
72
73
74
75
# File 'lib/acts_as_lockable_by/lockable.rb', line 68

def renew_lock(locked_by_id)
  # Set if only exist
  redis.eval(
    renew_lock_atomic_script,
    keys: [lock_key],
    argv: [locked_by_id, 'EX', ttl, 'XX']
  ) == 'OK'
end

#unlock(locked_by_id) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/acts_as_lockable_by/lockable.rb', line 56

def unlock(locked_by_id)
  redis.eval(
    unlock_atomic_script,
    keys: [lock_key],
    argv: [locked_by_id]
  ) == 1
end

#unlock!(locked_by_id) ⇒ Object



64
65
66
# File 'lib/acts_as_lockable_by/lockable.rb', line 64

def unlock!(locked_by_id)
  unlock(locked_by_id) || raise(UnLockError)
end