Class: Woods::SessionTracer::SolidCacheStore
- 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.
Constant Summary collapse
- KEY_PREFIX =
'woods:session:'- INDEX_KEY =
'woods:session_index'
Instance Method Summary collapse
-
#clear(session_id) ⇒ void
Remove all data for a single session.
-
#clear_all ⇒ void
Remove all session data.
-
#initialize(cache:, expires_in: nil) ⇒ SolidCacheStore
constructor
A new instance of SolidCacheStore.
-
#read(session_id) ⇒ Array<Hash>
Read all request records for a session.
-
#record(session_id, request_data) ⇒ void
Append a request record to a session (read-modify-write).
-
#sessions(limit: 20) ⇒ Array<Hash>
List recent session summaries.
Constructor Details
#initialize(cache:, expires_in: nil) ⇒ SolidCacheStore
Returns a new instance of SolidCacheStore.
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.
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_all ⇒ void
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.
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).
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.
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 |