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 |
# File 'lib/phronomy/memory/storage/in_memory.rb', line 13 def initialize @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
49 50 51 52 53 54 55 |
# File 'lib/phronomy/memory/storage/in_memory.rb', line 49 def append_raw(thread_id:, messages:, starting_seq:) now = Time.now @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 |
#clear(thread_id:) ⇒ Object
36 37 38 39 40 |
# File 'lib/phronomy/memory/storage/in_memory.rb', line 36 def clear(thread_id:) @store.delete(thread_id) clear_raw(thread_id: thread_id) clear_compactions(thread_id: thread_id) end |
#clear_compactions(thread_id:) ⇒ Object
88 89 90 |
# File 'lib/phronomy/memory/storage/in_memory.rb', line 88 def clear_compactions(thread_id:) @compaction_store.delete(thread_id) end |
#clear_raw(thread_id:) ⇒ Object
64 65 66 |
# File 'lib/phronomy/memory/storage/in_memory.rb', line 64 def clear_raw(thread_id:) @raw_store.delete(thread_id) end |
#load(thread_id:) ⇒ Array
25 26 27 |
# File 'lib/phronomy/memory/storage/in_memory.rb', line 25 def load(thread_id:) (@store[thread_id] || []).dup end |
#load_compactions(thread_id:) ⇒ Array<Hash>
83 84 85 |
# File 'lib/phronomy/memory/storage/in_memory.rb', line 83 def load_compactions(thread_id:) (@compaction_store[thread_id] || []).dup end |
#load_raw(thread_id:) ⇒ Array<Hash>
59 60 61 |
# File 'lib/phronomy/memory/storage/in_memory.rb', line 59 def load_raw(thread_id:) (@raw_store[thread_id] || []).dup end |
#purge_older_than(thread_id:, older_than:) ⇒ Object
Remove raw messages recorded before +older_than+ for this thread.
96 97 98 99 100 |
# File 'lib/phronomy/memory/storage/in_memory.rb', line 96 def purge_older_than(thread_id:, older_than:) return unless @raw_store[thread_id] @raw_store[thread_id].reject! { |entry| entry[:recorded_at] && entry[:recorded_at] < older_than } end |
#save(thread_id:, messages:) ⇒ Object
31 32 33 |
# File 'lib/phronomy/memory/storage/in_memory.rb', line 31 def save(thread_id:, messages:) @store[thread_id] = .dup end |
#save_compaction(thread_id:, start_seq:, end_seq:, summary_text:) ⇒ Object
76 77 78 79 |
# File 'lib/phronomy/memory/storage/in_memory.rb', line 76 def save_compaction(thread_id:, start_seq:, end_seq:, summary_text:) @compaction_store[thread_id] ||= [] @compaction_store[thread_id] << {start_seq: start_seq, end_seq: end_seq, summary_text: summary_text} end |