Module: Bogo::Memoization
- Defined in:
- lib/bogo/memoization.rb
Overview
Memoization helpers
Constant Summary collapse
- EXCLUSIVE_LOCK =
Lock for providing exclusive access
Mutex.new
- GLOBAL_MEMOS =
Holder for global memoization items
Smash.new
Class Method Summary collapse
-
.cleanup(object_id) ⇒ Proc
Clean up isolated memoizations.
-
.clear_current! ⇒ nil
Clear thread memoizations.
-
.clear_global! ⇒ nil
Clear global memoizations.
Instance Method Summary collapse
-
#_memo ⇒ Smash
Memoization hash for current thread.
-
#clear_memoizations! ⇒ TrueClass
Remove all memoized values.
-
#memoize(key, direct = false) { ... } ⇒ Object
Memoize data.
-
#memoized?(key, direct = false) ⇒ TrueClass, FalseClass
Check if memoization entry for given key exists.
-
#unmemoize(key, direct = false) ⇒ Object
Remove memoized value.
Class Method Details
.cleanup(object_id) ⇒ Proc
Clean up isolated memoizations
18 19 20 21 22 23 24 |
# File 'lib/bogo/memoization.rb', line 18 def cleanup(object_id) proc do Thread.current[:bogo_memoization].delete_if do |k,v| k.to_s.start_with?(object_id.to_s) end end end |
.clear_current! ⇒ nil
Clear thread memoizations
29 30 31 |
# File 'lib/bogo/memoization.rb', line 29 def clear_current! Thread.current[:bogo_memoization] = nil end |
.clear_global! ⇒ nil
Clear global memoizations
36 37 38 39 40 |
# File 'lib/bogo/memoization.rb', line 36 def clear_global! EXCLUSIVE_LOCK.synchronize do GLOBAL_MEMOS.clear end end |
Instance Method Details
#_memo ⇒ Smash
Returns memoization hash for current thread.
71 72 73 74 75 76 77 |
# File 'lib/bogo/memoization.rb', line 71 def _memo unless(Thread.current[:bogo_memoization]) Thread.current[:bogo_memoization] = Smash.new ObjectSpace.define_finalizer(self, Bogo::Memoization.cleanup(self.object_id)) end Thread.current[:bogo_memoization] end |
#clear_memoizations! ⇒ TrueClass
Remove all memoized values
118 119 120 121 122 123 124 125 |
# File 'lib/bogo/memoization.rb', line 118 def clear_memoizations! _memo.keys.find_all do |key| key.to_s.start_with?("#{self.object_id}_") end.each do |key| _memo.delete(key) end true end |
#memoize(key, direct = false) { ... } ⇒ Object
Memoize data
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/bogo/memoization.rb', line 51 def memoize(key, direct=false) unless(direct) key = "#{self.object_id}_#{key}" end if(direct == :global) EXCLUSIVE_LOCK.synchronize do unless(GLOBAL_MEMOS.has_key?(key)) GLOBAL_MEMOS[key] = yield end GLOBAL_MEMOS[key] end else unless(_memo.has_key?(key)) _memo[key] = yield end _memo[key] end end |
#memoized?(key, direct = false) ⇒ TrueClass, FalseClass
Check if memoization entry for given key exists
84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/bogo/memoization.rb', line 84 def memoized?(key, direct=false) unless(direct) key = "#{self.object_id}_#{key}" end if(direct == :global) EXCLUSIVE_LOCK.synchronize do GLOBAL_MEMOS.key?(key) end else _memo.key?(key) end end |
#unmemoize(key, direct = false) ⇒ Object
Remove memoized value
102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/bogo/memoization.rb', line 102 def unmemoize(key, direct=false) unless(direct) key = "#{self.object_id}_#{key}" end if(direct == :global) EXCLUSIVE_LOCK.synchronize do GLOBAL_MEMOS.delete(key) end else _memo.delete(key) end end |