Class: Phronomy::StateStore::InMemory

Inherits:
Base
  • Object
show all
Defined in:
lib/phronomy/state_store/in_memory.rb

Overview

In-memory state store backed by per-thread-id Actor instances from ThreadActorRegistry. Suitable for single-process use only.

Constant Summary collapse

THREAD_DATA_KEY =

Thread-local key for per-thread-id state data (namespaced by store instance object_id to support multiple independent InMemory stores).

:phronomy_state_store_in_memory_data

Instance Method Summary collapse

Constructor Details

#initializeInMemory

Returns a new instance of InMemory.



12
13
# File 'lib/phronomy/state_store/in_memory.rb', line 12

def initialize
end

Instance Method Details

#clear(thread_id) ⇒ self

Parameters:

  • thread_id (String)

Returns:

  • (self)


36
37
38
39
40
41
42
# File 'lib/phronomy/state_store/in_memory.rb', line 36

def clear(thread_id)
  store_id = object_id
  Phronomy::ThreadActorRegistry.for(thread_id).call do
    (Thread.current[THREAD_DATA_KEY] ||= {}).delete(store_id)
  end
  self
end

#clear_allObject



44
45
46
47
48
49
50
# File 'lib/phronomy/state_store/in_memory.rb', line 44

def clear_all
  store_id = object_id
  Phronomy::ThreadActorRegistry.each_actor do |actor|
    actor.call { (Thread.current[THREAD_DATA_KEY] ||= {}).delete(store_id) }
  end
  self
end

#load(thread_id) ⇒ Object?

Returns state object or nil.

Parameters:

  • thread_id (String)

Returns:

  • (Object, nil)

    state object or nil



27
28
29
30
31
32
# File 'lib/phronomy/state_store/in_memory.rb', line 27

def load(thread_id)
  store_id = object_id
  Phronomy::ThreadActorRegistry.for(thread_id).call do
    (Thread.current[THREAD_DATA_KEY] ||= {})[store_id]
  end
end

#save(state) ⇒ self

Parameters:

  • state (Object)

    includes Phronomy::WorkflowContext; must have a non-nil thread_id

Returns:

  • (self)


17
18
19
20
21
22
23
# File 'lib/phronomy/state_store/in_memory.rb', line 17

def save(state)
  store_id = object_id
  Phronomy::ThreadActorRegistry.for(state.thread_id).call do
    (Thread.current[THREAD_DATA_KEY] ||= {})[store_id] = state
  end
  self
end