Class: ConvertSdk::Stores::MemoryStore

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

Overview

The default in-process store: a plain +Hash+ guarded by a +Mutex+.

+MemoryStore+ is what DataStoreManager falls back to when no custom store is supplied (or a supplied store fails validation). It satisfies the duck-typed store contract — +#get+ / +#set+ — with both operations serialized through a single mutex so concurrent reads and writes from multiple threads cannot corrupt the underlying +Hash+ or lose a write.

== Per-process limitation

State lives only in this process's memory. It is NOT shared across processes, workers, or machines: sticky bucketing (Story 2.11) and goal deduplication (Story 4.3) that round-trip through a +MemoryStore+ are therefore consistent only within the lifetime of a single process. A forked web worker, a restarted process, or a second host each start with an empty store. For cross-process stickiness and dedup, supply a shared backing store — +RedisStore+ (Story 2.2) is the first-party option.

== Thread safety

All access to the backing +Hash+ is serialized by +@mutex+; there is no public path to the +Hash+ that bypasses the lock.

Instance Method Summary collapse

Constructor Details

#initializeMemoryStore

Returns a new instance of MemoryStore.



31
32
33
34
35
# File 'lib/convert_sdk/stores/memory_store.rb', line 31

def initialize
  # Thread safety: guarded by @mutex.
  @data = {}
  @mutex = Thread::Mutex.new
end

Instance Method Details

#get(key) ⇒ Object?

Read the value stored under +key+.

Parameters:

  • key (String)

    the lookup key.

Returns:

  • (Object, nil)

    the stored value, or +nil+ if the key is absent.



41
42
43
# File 'lib/convert_sdk/stores/memory_store.rb', line 41

def get(key)
  @mutex.synchronize { @data[key] }
end

#set(key, value) ⇒ Object

Store +value+ under +key+, overwriting any existing value.

Parameters:

  • key (String)

    the storage key.

  • value (Object)

    the value to store.

Returns:

  • (Object)

    the stored value.



50
51
52
# File 'lib/convert_sdk/stores/memory_store.rb', line 50

def set(key, value)
  @mutex.synchronize { @data[key] = value }
end