Module: LockAndCache
- Defined in:
- lib/lock_and_cache.rb,
lib/lock_and_cache/version.rb
Constant Summary collapse
- VERSION =
"0.0.2"
Instance Method Summary collapse
-
#lock_and_cache(method_id) ⇒ Object
only for instance methods.
Instance Method Details
#lock_and_cache(method_id) ⇒ Object
only for instance methods
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/lock_and_cache.rb', line 10 def lock_and_cache(method_id) unlocked_method_id = "_lock_and_cache_unlocked_#{method_id}" alias_method unlocked_method_id, method_id define_method method_id do |*args| lock_key = [self.class.name, method_id, HashDigest.digest3([as_cache_key]+args)].join('/') ActiveRecord::Base.with_advisory_lock(lock_key) do if cache_method_cached?(method_id, args) send method_id, *args # which will be the cached version else send unlocked_method_id end end end cache_method method_id end |