Class: Tina4::SessionHandlers::ValkeyHandler
- Inherits:
-
Object
- Object
- Tina4::SessionHandlers::ValkeyHandler
- Defined in:
- lib/tina4/session_handlers/valkey_handler.rb
Overview
Valkey-backed session handler. Valkey speaks the RESP protocol, so it works
through the redis gem when installed (parity with Python/Node); otherwise
it speaks raw RESP over a TCP socket via RespClient — zero dependencies.
Same as RedisHandler but reads VALKEY-prefixed configuration variables.
Instance Method Summary collapse
- #cleanup ⇒ Object
- #destroy(session_id) ⇒ Object
-
#initialize(options = {}) ⇒ ValkeyHandler
constructor
A new instance of ValkeyHandler.
- #read(session_id) ⇒ Object
-
#write(session_id, data, ttl = 0) ⇒ Object
Write session data.
Constructor Details
#initialize(options = {}) ⇒ ValkeyHandler
Returns a new instance of ValkeyHandler.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/tina4/session_handlers/valkey_handler.rb', line 12 def initialize( = {}) @prefix = [:prefix] || ENV["TINA4_SESSION_VALKEY_PREFIX"] || "tina4:session:" # TINA4_SESSION_VALKEY_TTL stays as the valkey-specific override, but it # now falls back to TINA4_SESSION_TTL - the ONE session-lifetime variable # every backend must honour (ADR-0024) - instead of a hard-coded 86400. # 3600 matches Python (the master), PHP and Node. @ttl = ([:ttl] || ENV["TINA4_SESSION_VALKEY_TTL"] || ENV["TINA4_SESSION_TTL"] || 3600).to_i @host = [:host] || ENV["TINA4_SESSION_VALKEY_HOST"] || "localhost" @port = [:port] || (ENV["TINA4_SESSION_VALKEY_PORT"] ? ENV["TINA4_SESSION_VALKEY_PORT"].to_i : 6379) @db = [:db] || (ENV["TINA4_SESSION_VALKEY_DB"] ? ENV["TINA4_SESSION_VALKEY_DB"].to_i : 0) @password = [:password] || ENV["TINA4_SESSION_VALKEY_PASSWORD"] @redis = build_gem_client @resp = @redis ? nil : RespClient.new(host: @host, port: @port, password: @password, db: @db) end |
Instance Method Details
#cleanup ⇒ Object
61 62 63 |
# File 'lib/tina4/session_handlers/valkey_handler.rb', line 61 def cleanup # Valkey handles TTL automatically (same as Redis) end |
#destroy(session_id) ⇒ Object
56 57 58 59 |
# File 'lib/tina4/session_handlers/valkey_handler.rb', line 56 def destroy(session_id) key = "#{@prefix}#{session_id}" @redis ? @redis.del(key) : @resp.del(key) end |
#read(session_id) ⇒ Object
27 28 29 30 31 32 33 34 |
# File 'lib/tina4/session_handlers/valkey_handler.rb', line 27 def read(session_id) key = "#{@prefix}#{session_id}" data = @redis ? @redis.get(key) : @resp.get(key) return nil unless data JSON.parse(data) rescue JSON::ParserError nil end |
#write(session_id, data, ttl = 0) ⇒ Object
Write session data. A per-call ttl WINS over the handler default, so
asking for a 60s session really gets 60s; 0 uses the default. Every
handler in every Tina4 framework takes this third argument -- Session#write
passes it, and a handler that did not accept it raised ArgumentError,
which safe_write swallowed into a silent STALE write.
45 46 47 48 49 50 51 52 53 54 |
# File 'lib/tina4/session_handlers/valkey_handler.rb', line 45 def write(session_id, data, ttl = 0) key = "#{@prefix}#{session_id}" payload = JSON.generate(data) effective_ttl = ttl.to_i.positive? ? ttl.to_i : @ttl if @redis @redis.setex(key, effective_ttl, payload) else @resp.setex(key, effective_ttl, payload) end end |