Module: LockMethod::ClassMethods
- Defined in:
- lib/lock_method.rb
Overview
All Classes (but not instances), get the .lock_method method.
Instance Method Summary collapse
-
#lock_method(*args) ⇒ Object
Lock a method.
Instance Method Details
#lock_method(*args) ⇒ Object
Lock a method.
Options:
-
:ttlTTL in seconds, defaults to whatever’s in LockMethod.config.default_ttl -
:spinWhether to wait indefinitely for another lock to expire
Note 2: Check out LockMethod.config.default_ttl… the default is 24 hours!
Example:
class Blog
# [...]
def get_latest_entries
sleep 5
end
# [...]
lock_method :get_latest_entries
# if you wanted a different ttl...
# lock_method :get_latest_entries, 800 #seconds
end
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/lock_method.rb', line 58 def lock_method(*args) = args. = .symbolize_keys method_id = args.first if args.last.is_a?(::Numeric) [:ttl] ||= args.last end original_method_id = "_unlocked_#{method_id}" alias_method original_method_id, method_id define_method method_id do |*args1| = .merge(:args => args1) lock = ::LockMethod::Lock.new self, method_id, lock.call_and_lock(*([original_method_id]+args1)) end end |