Class: Findable::Query::Lock
- Inherits:
-
Object
- Object
- Findable::Query::Lock
- Includes:
- Connection
- Defined in:
- lib/findable/query/lock.rb
Instance Method Summary collapse
-
#initialize(lock_key, thread_key, options = {}) ⇒ Lock
constructor
A new instance of Lock.
- #lock ⇒ Object
- #unlock! ⇒ Object
Methods included from Connection
Constructor Details
#initialize(lock_key, thread_key, options = {}) ⇒ Lock
Returns a new instance of Lock.
8 9 10 11 12 |
# File 'lib/findable/query/lock.rb', line 8 def initialize(lock_key, thread_key, = {}) @lock_key = lock_key @thread_key = thread_key @options = .symbolize_keys! end |
Instance Method Details
#lock ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/findable/query/lock.rb', line 14 def lock if Thread.current[@thread_key] yield else Thread.current[@thread_key] = true try_lock!(Time.current) begin yield ensure Thread.current[@thread_key] = nil unlock! end end end |
#unlock! ⇒ Object
29 30 31 |
# File 'lib/findable/query/lock.rb', line 29 def unlock! redis.del(@lock_key) end |