Class: MemoizeUntil::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/memoize_until/store.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(kind) ⇒ Store

Returns a new instance of Store.



7
8
9
10
11
# File 'lib/memoize_until/store.rb', line 7

def initialize(kind)
  @_store = {}
  @_kind = kind
  @_mutex = Mutex.new
end

Instance Attribute Details

#_kindObject (readonly)

Returns the value of attribute _kind.



5
6
7
# File 'lib/memoize_until/store.rb', line 5

def _kind
  @_kind
end

#_mutexObject (readonly)

Returns the value of attribute _mutex.



5
6
7
# File 'lib/memoize_until/store.rb', line 5

def _mutex
  @_mutex
end

#_storeObject (readonly)

Returns the value of attribute _store.



5
6
7
# File 'lib/memoize_until/store.rb', line 5

def _store
  @_store
end

Instance Method Details

#add(key) ⇒ Object

add runtime keys



29
30
31
32
33
# File 'lib/memoize_until/store.rb', line 29

def add(key)
  _mutex.synchronize do
    _store[key] ||= {}
  end
end

#clear_all(key) ⇒ Object

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



38
39
40
41
42
# File 'lib/memoize_until/store.rb', line 38

def clear_all(key)
  _mutex.synchronize do
    _store[key] = {}
  end
end

#clear_now(key) ⇒ Object

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



47
48
49
50
# File 'lib/memoize_until/store.rb', line 47

def clear_now(key)
  now = Time.now.public_send(_kind)
  set(key, now, nil)
end

#fetch(key) ⇒ Object

returns the value from memory if already memoized for "now" for the given key else evaluates the block and memoizes that caches nils too



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/memoize_until/store.rb', line 16

def fetch(key)
  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