class MemoizeUntil::Store
Attributes
_kind[R]
_mutex[R]
_store[R]
Public Class Methods
new(kind)
click to toggle source
# File lib/memoize_until/store.rb, line 5 def initialize(kind) @_store = {} @_kind = kind @_mutex = Mutex.new end
Public Instance Methods
add(key)
click to toggle source
add runtime keys
# File lib/memoize_until/store.rb, line 27 def add(key) _mutex.synchronize do _store[key] ||= {} end end
clear_all(key)
click to toggle source
clears all previously memoized values for the given key only clears memory in the process that this code runs. added for supporting fetch and custom scripts
# File lib/memoize_until/store.rb, line 36 def clear_all(key) _mutex.synchronize do _store[key] = {} end end
clear_now(key)
click to toggle source
clears previously memoized value for “now” for the given key only clears memory in the process that this code runs on. added for supporting custom scripts / test cases
# File lib/memoize_until/store.rb, line 45 def clear_now(key) now = Time.now.public_send(_kind) set(key, now, nil) end
fetch(key) { || ... }
click to toggle source
returns the value from memory if already memoized for “now” for the given key else evaluates the block and memoizes that caches nils too
# File lib/memoize_until/store.rb, line 14 def fetch(key, &block) now = Time.now.public_send(_kind) value = get(key, now) if value.nil? clear_all(key) value = set(key, now, set_nil(yield)) end unset_nil(value) end
Private Instance Methods
get(key, now)
click to toggle source
# File lib/memoize_until/store.rb, line 68 def get(key, now) _mutex.synchronize do purpose = _store[key] raise NotImplementedError unless purpose purpose[now] end end
set(key, now, value)
click to toggle source
# File lib/memoize_until/store.rb, line 62 def set(key, now, value) _mutex.synchronize do _store[key][now] = value end end
set_nil(value)
click to toggle source
caches nils through a pseudo object
# File lib/memoize_until/store.rb, line 53 def set_nil(value) value.nil? ? NullObject.instance : value end
unset_nil(value)
click to toggle source
replaces cached pseudo object and returns nil
# File lib/memoize_until/store.rb, line 58 def unset_nil(value) value.is_a?(NullObject) ? nil : value end