Class: Woods::SessionTracer::SolidCacheStore

Inherits:
Store
  • Object
show all
Defined in:
lib/woods/session_tracer/solid_cache_store.rb

Overview

SolidCache-backed session store.

Uses SolidCache key-value storage with ‘expires_in`. Single JSON blob per session (read-modify-write pattern). Requires the `solid_cache` gem.

Examples:

store = SolidCacheStore.new(cache: SolidCache::Store.new, expires_in: 3600)
store.record("abc123", { controller: "OrdersController", action: "create" })

Constant Summary collapse

KEY_PREFIX =
'woods:session:'
INDEX_KEY =
'woods:session_index'

Instance Method Summary collapse

Constructor Details

#initialize(cache:, expires_in: nil) ⇒ SolidCacheStore

Returns a new instance of SolidCacheStore.

Parameters:

  • cache (ActiveSupport::Cache::Store)

    A SolidCache (or compatible) cache instance

  • expires_in (Integer, nil) (defaults to: nil)

    Expiry time in seconds (nil = no expiry)



23
24
25
26
27
# File 'lib/woods/session_tracer/solid_cache_store.rb', line 23

def initialize(cache:, expires_in: nil)
  super()
  @cache = cache
  @expires_in = expires_in
end

Instance Method Details

#clear(session_id) ⇒ void

This method returns an undefined value.

Remove all data for a single session.

Parameters:

  • session_id (String)

    The session identifier



84
85
86
87
88
89
# File 'lib/woods/session_tracer/solid_cache_store.rb', line 84

def clear(session_id)
  @cache.delete(session_key(session_id))
  index = read_index
  index.delete(session_id)
  write_index(index)
end

#clear_allvoid

This method returns an undefined value.

Remove all session data.



94
95
96
97
98
# File 'lib/woods/session_tracer/solid_cache_store.rb', line 94

def clear_all
  index = read_index
  index.each { |id| @cache.delete(session_key(id)) }
  @cache.delete(INDEX_KEY)
end

#read(session_id) ⇒ Array<Hash>

Read all request records for a session.

Parameters:

  • session_id (String)

    The session identifier

Returns:

  • (Array<Hash>)

    Request records, oldest first



54
55
56
57
58
59
60
61
62
# File 'lib/woods/session_tracer/solid_cache_store.rb', line 54

def read(session_id)
  key = session_key(session_id)
  raw = @cache.read(key)
  return [] unless raw

  JSON.parse(raw)
rescue JSON::ParserError
  []
end

#record(session_id, request_data) ⇒ void

This method returns an undefined value.

Append a request record to a session (read-modify-write).

NOTE: Not atomic — concurrent writes to the same session may lose data. Acceptable for development tracing. For high-concurrency tracing, use RedisStore (RPUSH is atomic) or FileStore (LOCK_EX).

Parameters:

  • session_id (String)

    The session identifier

  • request_data (Hash)

    Request metadata to store



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/woods/session_tracer/solid_cache_store.rb', line 38

def record(session_id, request_data)
  key = session_key(session_id)
  existing = @cache.read(key)
  requests = existing ? JSON.parse(existing) : []
  requests << request_data

  write_opts = @expires_in ? { expires_in: @expires_in } : {}
  @cache.write(key, JSON.generate(requests), **write_opts)

  update_index(session_id)
end

#sessions(limit: 20) ⇒ Array<Hash>

List recent session summaries.

Parameters:

  • limit (Integer) (defaults to: 20)

    Maximum number of sessions to return

Returns:

  • (Array<Hash>)

    Session summaries



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/woods/session_tracer/solid_cache_store.rb', line 68

def sessions(limit: 20)
  index = read_index
  active = index.select { |id| @cache.exist?(session_key(id)) }

  # Clean up expired entries from the index
  write_index(active) if active.size != index.size

  active.first(limit).map do |session_id|
    session_summary(session_id, read(session_id))
  end
end