Class: Phronomy::StateStore::InMemory

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

Overview

Thread-safe in-process state store backed by a plain Ruby Hash.

Used as the recommended default for single-process applications and tests. State does not survive process restart.

Examples:

store = Phronomy::StateStore::InMemory.new
store.save("t1", { fields: { count: 1 }, phase: "__end__" })
store.load("t1")   # => { fields: { count: 1 }, phase: "__end__" }
store.delete("t1")
store.load("t1")   # => nil

Instance Method Summary collapse

Constructor Details

#initializeInMemory

Returns a new instance of InMemory.



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

def initialize
  @data = {}
  @mutex = Mutex.new
end

Instance Method Details

#delete(thread_id) ⇒ void

This method returns an undefined value.

Parameters:

  • thread_id (String)


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

def delete(thread_id)
  @mutex.synchronize { @data.delete(thread_id) }
  nil
end

#load(thread_id) ⇒ Hash?

Parameters:

  • thread_id (String)

Returns:

  • (Hash, nil)


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

def load(thread_id)
  @mutex.synchronize do
    snap = @data[thread_id]
    snap ? deep_dup(snap) : nil
  end
end

#save(thread_id, snapshot) ⇒ void

This method returns an undefined value.

Parameters:

  • thread_id (String)
  • snapshot (Hash)


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

def save(thread_id, snapshot)
  @mutex.synchronize { @data[thread_id] = deep_dup(snapshot) }
  nil
end