Class: X402::BSV::TxidStore::Memory

Inherits:
Object
  • Object
show all
Defined in:
lib/x402/bsv/txid_store.rb

Overview

In-memory backend suitable for development and single-process deployments. Thread-safe via Monitor. Entries expire after +ttl+ seconds.

NOTE: This store provides no deduplication across OS processes. Production multi-process deployments should use a shared backend.

Constant Summary collapse

DEFAULT_TTL =
600
DEFAULT_MAX_SIZE =
10_000

Instance Method Summary collapse

Constructor Details

#initialize(ttl: DEFAULT_TTL, max_size: DEFAULT_MAX_SIZE) ⇒ Memory

Returns a new instance of Memory.

Parameters:

  • ttl (Integer) (defaults to: DEFAULT_TTL)

    seconds before a seen txid expires (default 600)

  • max_size (Integer) (defaults to: DEFAULT_MAX_SIZE)

    cap on stored txids (default 10_000)



25
26
27
28
29
30
# File 'lib/x402/bsv/txid_store.rb', line 25

def initialize(ttl: DEFAULT_TTL, max_size: DEFAULT_MAX_SIZE)
  @entries = {}
  @monitor = Monitor.new
  @ttl = ttl
  @max_size = max_size
end

Instance Method Details

#record_if_unseen!(txid) ⇒ Object

Atomically checks and records a txid. Returns true if the txid was new (now recorded). Returns false if already seen (duplicate).



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/x402/bsv/txid_store.rb', line 35

def record_if_unseen!(txid)
  @monitor.synchronize do
    purge_expired!

    entry = @entries[txid]
    return false if entry && !expired?(entry)

    @entries[txid] = monotonic_now
    evict_oldest! if @entries.size > @max_size
    true
  end
end