Class: QueuePulse::MemoryStore

Inherits:
Object
  • Object
show all
Defined in:
lib/queue_pulse/memory_store.rb

Overview

Minimal in-process cache used as a fallback when Rails.cache is unavailable (e.g. plain Ruby tests, or apps with no cache store configured).

Implements the small subset of the ActiveSupport::Cache interface QueuePulse relies on: #read, #write (with :expires_in), #delete. Thread-safe.

Defined Under Namespace

Classes: Entry

Instance Method Summary collapse

Constructor Details

#initializeMemoryStore

Returns a new instance of MemoryStore.



12
13
14
15
# File 'lib/queue_pulse/memory_store.rb', line 12

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

Instance Method Details

#delete(key) ⇒ Object



37
38
39
40
# File 'lib/queue_pulse/memory_store.rb', line 37

def delete(key)
  @mutex.synchronize { @data.delete(key) }
  true
end

#read(key) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/queue_pulse/memory_store.rb', line 17

def read(key)
  @mutex.synchronize do
    entry = @data[key]
    return nil unless entry
    if entry.expires_at && entry.expires_at <= monotonic_now
      @data.delete(key)
      return nil
    end
    entry.value
  end
end

#write(key, value, expires_in: nil) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/queue_pulse/memory_store.rb', line 29

def write(key, value, expires_in: nil)
  @mutex.synchronize do
    expires_at = expires_in ? monotonic_now + expires_in : nil
    @data[key] = Entry.new(value, expires_at)
  end
  true
end