Class: Phronomy::Memory::Storage::InMemory
- Defined in:
- lib/phronomy/memory/storage/in_memory.rb
Overview
In-process Hash-backed storage for conversation messages. Messages are lost when the process exits.
Instance Method Summary collapse
- #append_raw(thread_id:, messages:, starting_seq:) ⇒ Object
- #clear(thread_id:) ⇒ Object
- #clear_compactions(thread_id:) ⇒ Object
- #clear_raw(thread_id:) ⇒ Object
-
#initialize ⇒ InMemory
constructor
A new instance of InMemory.
- #load(thread_id:) ⇒ Array
- #load_compactions(thread_id:) ⇒ Array<Hash>
- #load_raw(thread_id:) ⇒ Array<Hash>
-
#purge_older_than(thread_id:, older_than:) ⇒ Object
Remove raw messages recorded before +older_than+ for this thread.
- #save(thread_id:, messages:) ⇒ Object
- #save_compaction(thread_id:, start_seq:, end_seq:, summary_text:) ⇒ Object
Methods inherited from Base
Constructor Details
#initialize ⇒ InMemory
Returns a new instance of InMemory.
13 14 15 16 17 18 |
# File 'lib/phronomy/memory/storage/in_memory.rb', line 13 def initialize @mutex = Mutex.new @store = {} @raw_store = {} # thread_id => [{seq:, message:}, ...] @compaction_store = {} # thread_id => [{start_seq:, end_seq:, summary_text:}, ...] end |
Instance Method Details
#append_raw(thread_id:, messages:, starting_seq:) ⇒ Object
52 53 54 55 56 57 58 59 60 |
# File 'lib/phronomy/memory/storage/in_memory.rb', line 52 def append_raw(thread_id:, messages:, starting_seq:) now = Time.now @mutex.synchronize do @raw_store[thread_id] ||= [] .each_with_index do |msg, i| @raw_store[thread_id] << {seq: starting_seq + i, message: msg, recorded_at: now} end end end |
#clear(thread_id:) ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/phronomy/memory/storage/in_memory.rb', line 37 def clear(thread_id:) @mutex.synchronize do @store.delete(thread_id) @raw_store.delete(thread_id) @compaction_store.delete(thread_id) end end |
#clear_compactions(thread_id:) ⇒ Object
95 96 97 |
# File 'lib/phronomy/memory/storage/in_memory.rb', line 95 def clear_compactions(thread_id:) @mutex.synchronize { @compaction_store.delete(thread_id) } end |
#clear_raw(thread_id:) ⇒ Object
69 70 71 |
# File 'lib/phronomy/memory/storage/in_memory.rb', line 69 def clear_raw(thread_id:) @mutex.synchronize { @raw_store.delete(thread_id) } end |
#load(thread_id:) ⇒ Array
26 27 28 |
# File 'lib/phronomy/memory/storage/in_memory.rb', line 26 def load(thread_id:) @mutex.synchronize { (@store[thread_id] || []).dup } end |
#load_compactions(thread_id:) ⇒ Array<Hash>
90 91 92 |
# File 'lib/phronomy/memory/storage/in_memory.rb', line 90 def load_compactions(thread_id:) @mutex.synchronize { (@compaction_store[thread_id] || []).dup } end |
#load_raw(thread_id:) ⇒ Array<Hash>
64 65 66 |
# File 'lib/phronomy/memory/storage/in_memory.rb', line 64 def load_raw(thread_id:) @mutex.synchronize { (@raw_store[thread_id] || []).dup } end |
#purge_older_than(thread_id:, older_than:) ⇒ Object
Remove raw messages recorded before +older_than+ for this thread.
103 104 105 106 107 108 109 |
# File 'lib/phronomy/memory/storage/in_memory.rb', line 103 def purge_older_than(thread_id:, older_than:) @mutex.synchronize do next unless @raw_store[thread_id] @raw_store[thread_id].reject! { |entry| entry[:recorded_at] && entry[:recorded_at] < older_than } end end |
#save(thread_id:, messages:) ⇒ Object
32 33 34 |
# File 'lib/phronomy/memory/storage/in_memory.rb', line 32 def save(thread_id:, messages:) @mutex.synchronize { @store[thread_id] = .dup } end |
#save_compaction(thread_id:, start_seq:, end_seq:, summary_text:) ⇒ Object
81 82 83 84 85 86 |
# File 'lib/phronomy/memory/storage/in_memory.rb', line 81 def save_compaction(thread_id:, start_seq:, end_seq:, summary_text:) @mutex.synchronize do @compaction_store[thread_id] ||= [] @compaction_store[thread_id] << {start_seq: start_seq, end_seq: end_seq, summary_text: summary_text} end end |