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) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ ValkeyHandler
Returns a new instance of ValkeyHandler.
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/tina4/session_handlers/valkey_handler.rb', line 12 def initialize( = {}) @prefix = [:prefix] || ENV["TINA4_SESSION_VALKEY_PREFIX"] || "tina4:session:" @ttl = [:ttl] || (ENV["TINA4_SESSION_VALKEY_TTL"] ? ENV["TINA4_SESSION_VALKEY_TTL"].to_i : 86400) @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
47 48 49 |
# File 'lib/tina4/session_handlers/valkey_handler.rb', line 47 def cleanup # Valkey handles TTL automatically (same as Redis) end |
#destroy(session_id) ⇒ Object
42 43 44 45 |
# File 'lib/tina4/session_handlers/valkey_handler.rb', line 42 def destroy(session_id) key = "#{@prefix}#{session_id}" @redis ? @redis.del(key) : @resp.del(key) end |
#read(session_id) ⇒ Object
23 24 25 26 27 28 29 30 |
# File 'lib/tina4/session_handlers/valkey_handler.rb', line 23 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) ⇒ Object
32 33 34 35 36 37 38 39 40 |
# File 'lib/tina4/session_handlers/valkey_handler.rb', line 32 def write(session_id, data) key = "#{@prefix}#{session_id}" payload = JSON.generate(data) if @redis @redis.setex(key, @ttl, payload) else @resp.setex(key, @ttl, payload) end end |