Class: SolidObserver::CacheEventBuffer

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/solid_observer/cache_event_buffer.rb

Instance Method Summary collapse

Constructor Details

#initializeCacheEventBuffer

Returns a new instance of CacheEventBuffer.



9
10
11
12
# File 'lib/solid_observer/cache_event_buffer.rb', line 9

def initialize
  @mutex = Mutex.new
  @buffer = []
end

Instance Method Details

#clearObject



40
41
42
# File 'lib/solid_observer/cache_event_buffer.rb', line 40

def clear
  @mutex.synchronize { @buffer.clear }
end

#flush!Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/solid_observer/cache_event_buffer.rb', line 22

def flush!
  events = @mutex.synchronize do
    buffered_events = @buffer.dup
    @buffer.clear
    buffered_events
  end
  return if events.empty?

  Services::FlushCacheEventBuffer.call(events)
rescue => error
  @mutex.synchronize { @buffer = events + @buffer }
  Rails.logger&.error("[SolidObserver] Cache buffer flush failed: #{error.message}") if defined?(Rails)
end

#push(event_data) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/solid_observer/cache_event_buffer.rb', line 14

def push(event_data)
  config = SolidObserver.config
  return unless config.persistence_mode?

  should_flush = buffer_ready_to_flush?(event_data, config.buffer_size)
  flush! if should_flush
end

#sizeObject



36
37
38
# File 'lib/solid_observer/cache_event_buffer.rb', line 36

def size
  @mutex.synchronize { @buffer.size }
end