Class: Turbo::Replay::Repo::Memory
- Defined in:
- lib/turbo/replay/repo/memory.rb
Instance Method Summary collapse
- #get_all_messages(broadcasting:) ⇒ Object
- #get_current_sequence_number(broadcasting:) ⇒ Object
-
#initialize ⇒ Memory
constructor
A new instance of Memory.
- #insert_message(broadcasting:, content:, retention:) ⇒ Object
Constructor Details
#initialize ⇒ Memory
Returns a new instance of Memory.
4 5 6 7 8 9 |
# File 'lib/turbo/replay/repo/memory.rb', line 4 def initialize @mutex = Mutex.new @counters = {} = {} @ttl = {} end |
Instance Method Details
#get_all_messages(broadcasting:) ⇒ Object
17 18 19 20 21 |
# File 'lib/turbo/replay/repo/memory.rb', line 17 def (broadcasting:) synchronize(broadcasting) do .fetch(broadcasting, []) end end |
#get_current_sequence_number(broadcasting:) ⇒ Object
11 12 13 14 15 |
# File 'lib/turbo/replay/repo/memory.rb', line 11 def get_current_sequence_number(broadcasting:) synchronize(broadcasting) do @counters.fetch(broadcasting, 0) end end |
#insert_message(broadcasting:, content:, retention:) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/turbo/replay/repo/memory.rb', line 23 def (broadcasting:, content:, retention:) synchronize(broadcasting) do @ttl[broadcasting] = Time.current + retention.ttl next_sequence_number = (@counters[broadcasting] = @counters.fetch(broadcasting, 0) + 1) content_with_sequence_number = {sequence_number: next_sequence_number, content: content} ([broadcasting] ||= []).tap do || << content_with_sequence_number .shift if .length > retention.size end content_with_sequence_number end end |