Class: Smith::PersistenceAdapters::Memory
- Inherits:
-
Object
- Object
- Smith::PersistenceAdapters::Memory
- Defined in:
- lib/smith/persistence_adapters/memory.rb
Overview
In-process Hash adapter. Thread-safe via Monitor. No I/O, no transient errors. Designed for tests and quick smoke runs.
Tracks TTL via stamped expiry times so it behaves consistently with
other adapters' TTL semantics. Implements store_versioned via the
monitor, enabling optimistic-locking tests without Redis.
Auto-selected by Smith.persistence_adapter when both Smith.config.persistence_adapter is nil AND Smith.config.test_mode is true (typically set in spec_helper.rb).
Instance Method Summary collapse
- #clear! ⇒ Object
- #delete(key) ⇒ Object
- #fetch(key) ⇒ Object
-
#initialize ⇒ Memory
constructor
A new instance of Memory.
- #last_heartbeat(key) ⇒ Object
- #record_heartbeat(key, ttl: Smith.config.persistence_ttl) ⇒ Object
- #store(key, payload, ttl: Smith.config.persistence_ttl) ⇒ Object
-
#store_versioned(key, payload, expected_version:, ttl: Smith.config.persistence_ttl) ⇒ Object
Optimistic locking via Monitor-synchronized version compare.
- #transaction_open? ⇒ Boolean
Constructor Details
#initialize ⇒ Memory
Returns a new instance of Memory.
18 19 20 21 22 |
# File 'lib/smith/persistence_adapters/memory.rb', line 18 def initialize @store = {} @heartbeats = {} @monitor = Monitor.new end |
Instance Method Details
#clear! ⇒ Object
91 92 93 |
# File 'lib/smith/persistence_adapters/memory.rb', line 91 def clear! @monitor.synchronize { @store.clear } end |
#delete(key) ⇒ Object
39 40 41 42 43 44 |
# File 'lib/smith/persistence_adapters/memory.rb', line 39 def delete(key) @monitor.synchronize do @store.delete(key) @heartbeats.delete(key) end end |
#fetch(key) ⇒ Object
30 31 32 33 34 35 36 37 |
# File 'lib/smith/persistence_adapters/memory.rb', line 30 def fetch(key) @monitor.synchronize do entry = live_entry(key) next nil if entry.nil? copy_payload(entry[:payload]) end end |
#last_heartbeat(key) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/smith/persistence_adapters/memory.rb', line 54 def last_heartbeat(key) @monitor.synchronize do entry = @heartbeats[key] next nil if entry.nil? if entry[:expires_at] && entry[:expires_at] < Time.now.utc @heartbeats.delete(key) next nil end entry[:at] end end |
#record_heartbeat(key, ttl: Smith.config.persistence_ttl) ⇒ Object
48 49 50 51 52 |
# File 'lib/smith/persistence_adapters/memory.rb', line 48 def record_heartbeat(key, ttl: Smith.config.persistence_ttl) @monitor.synchronize do @heartbeats[key] = { at: Time.now.utc, expires_at: ttl ? Time.now.utc + ttl : nil } end end |
#store(key, payload, ttl: Smith.config.persistence_ttl) ⇒ Object
24 25 26 27 28 |
# File 'lib/smith/persistence_adapters/memory.rb', line 24 def store(key, payload, ttl: Smith.config.persistence_ttl) @monitor.synchronize do @store[key] = entry_for(payload, ttl) end end |
#store_versioned(key, payload, expected_version:, ttl: Smith.config.persistence_ttl) ⇒ Object
Optimistic locking via Monitor-synchronized version compare.
Raises Smith::PersistenceVersionConflict when the stored payload's
version differs from expected_version. The version is read from
the payload's JSON persistence_version field (same shape Redis
and ActiveRecord stores use, so the contract is consistent
across all versioned adapters).
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/smith/persistence_adapters/memory.rb', line 74 def store_versioned(key, payload, expected_version:, ttl: Smith.config.persistence_ttl) @monitor.synchronize do entry = live_entry(key) if entry current_version = parse_version(entry[:payload]) if current_version != expected_version raise Smith::PersistenceVersionConflict.new( key: key, expected: expected_version, actual: current_version ) end else VersionExpectation.validate_missing!(key, expected_version) end @store[key] = entry_for(payload, ttl) end end |
#transaction_open? ⇒ Boolean
46 |
# File 'lib/smith/persistence_adapters/memory.rb', line 46 def transaction_open? = false |