Class: LockMethod::Lock
- Inherits:
-
Object
- Object
- LockMethod::Lock
- Defined in:
- lib/lock_method/lock.rb
Overview
:nodoc: all
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#args_digest ⇒ Object
readonly
Returns the value of attribute args_digest.
-
#blk ⇒ Object
readonly
Returns the value of attribute blk.
-
#method_id ⇒ Object
readonly
Returns the value of attribute method_id.
-
#method_signature ⇒ Object
readonly
Returns the value of attribute method_signature.
-
#obj ⇒ Object
readonly
Returns the value of attribute obj.
-
#original_method_id ⇒ Object
readonly
Returns the value of attribute original_method_id.
-
#ttl ⇒ Object
readonly
Returns the value of attribute ttl.
Instance Method Summary collapse
- #call_and_lock ⇒ Object
- #delete ⇒ Object
-
#initialize(obj, method_id, args = nil, ttl = LockMethod.config.default_ttl, spin = false, &blk) ⇒ Lock
constructor
A new instance of Lock.
- #locked? ⇒ Boolean
- #marshal_dump ⇒ Object
- #marshal_load(source) ⇒ Object
- #save ⇒ Object
Constructor Details
#initialize(obj, method_id, args = nil, ttl = LockMethod.config.default_ttl, spin = false, &blk) ⇒ Lock
Returns a new instance of Lock.
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/lock_method/lock.rb', line 14 def initialize(obj, method_id, args = nil, ttl = LockMethod.config.default_ttl, spin = false, &blk) @mutex = ::Mutex.new @obj = obj @method_id = method_id @original_method_id = LockMethod.original_method_id method_id @method_signature = LockMethod.method_signature obj, method_id @args = args @args_digest = args.to_a.empty? ? 'empty' : LockMethod.digest(args) @ttl = ttl #!!!!! @spin = spin @blk = blk end |
Instance Attribute Details
#args ⇒ Object (readonly)
Returns the value of attribute args.
7 8 9 |
# File 'lib/lock_method/lock.rb', line 7 def args @args end |
#args_digest ⇒ Object (readonly)
Returns the value of attribute args_digest.
12 13 14 |
# File 'lib/lock_method/lock.rb', line 12 def args_digest @args_digest end |
#blk ⇒ Object (readonly)
Returns the value of attribute blk.
8 9 10 |
# File 'lib/lock_method/lock.rb', line 8 def blk @blk end |
#method_id ⇒ Object (readonly)
Returns the value of attribute method_id.
6 7 8 |
# File 'lib/lock_method/lock.rb', line 6 def method_id @method_id end |
#method_signature ⇒ Object (readonly)
Returns the value of attribute method_signature.
10 11 12 |
# File 'lib/lock_method/lock.rb', line 10 def method_signature @method_signature end |
#obj ⇒ Object (readonly)
Returns the value of attribute obj.
5 6 7 |
# File 'lib/lock_method/lock.rb', line 5 def obj @obj end |
#original_method_id ⇒ Object (readonly)
Returns the value of attribute original_method_id.
9 10 11 |
# File 'lib/lock_method/lock.rb', line 9 def original_method_id @original_method_id end |
#ttl ⇒ Object (readonly)
Returns the value of attribute ttl.
11 12 13 |
# File 'lib/lock_method/lock.rb', line 11 def ttl @ttl end |
Instance Method Details
#call_and_lock ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/lock_method/lock.rb', line 47 def call_and_lock while locked? and spin? ::Kernel.sleep 0.5 end if locked? raise Locked, %{#{method_signature} is currently locked.} else begin save obj.send(*([original_method_id]+args), &blk) ensure delete end end end |
#delete ⇒ Object
27 28 29 |
# File 'lib/lock_method/lock.rb', line 27 def delete LockMethod.config.storage.delete cache_key end |
#locked? ⇒ Boolean
35 36 37 |
# File 'lib/lock_method/lock.rb', line 35 def locked? !!LockMethod.config.storage.get(cache_key) end |
#marshal_dump ⇒ Object
39 40 41 |
# File 'lib/lock_method/lock.rb', line 39 def marshal_dump [] end |
#marshal_load(source) ⇒ Object
43 44 45 |
# File 'lib/lock_method/lock.rb', line 43 def marshal_load(source) # nothing end |
#save ⇒ Object
31 32 33 |
# File 'lib/lock_method/lock.rb', line 31 def save LockMethod.config.storage.set cache_key, self, ttl end |