Class: Tina4::SessionHandlers::RedisHandler
- Inherits:
-
Object
- Object
- Tina4::SessionHandlers::RedisHandler
- Defined in:
- lib/tina4/session_handlers/redis_handler.rb
Overview
Redis-backed session handler. Prefers the redis gem when it is installed
(parity with Python, which prefers redis-py the same way, INSIDE this one
handler rather than as a separate backend name — Node's redis-npm
backend did the latter and was retired 2026-07-31 as drift); otherwise
speaks raw RESP over a TCP socket via RespClient — zero dependencies, so a
Tina4 app stores sessions in Redis with no extra gem.
Instance Method Summary collapse
- #cleanup ⇒ Object
- #destroy(session_id) ⇒ Object
-
#initialize(options = {}) ⇒ RedisHandler
constructor
Connection is configured from TINA4_SESSION_REDIS_* env vars (parity with Python's RedisSessionHandler and the Ruby ValkeyHandler shape) so TINA4_SESSION_BACKEND=redis can actually be pointed at a server by env.
- #read(session_id) ⇒ Object
-
#write(session_id, data, ttl = 0) ⇒ Object
Write session data.
Constructor Details
#initialize(options = {}) ⇒ RedisHandler
Connection is configured from TINA4_SESSION_REDIS_* env vars (parity with Python's RedisSessionHandler and the Ruby ValkeyHandler shape) so TINA4_SESSION_BACKEND=redis can actually be pointed at a server by env. An explicit constructor option always wins over the environment.
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/tina4/session_handlers/redis_handler.rb', line 18 def initialize( = {}) @prefix = [:prefix] || ENV["TINA4_SESSION_REDIS_PREFIX"] || "tina4:session:" # TINA4_SESSION_TTL reaches every backend (ADR-0024); was a hard-coded # 86400. 3600 matches Python (the master), PHP and Node. @ttl = ([:ttl] || ENV["TINA4_SESSION_TTL"] || 3600).to_i @host = [:host] || ENV["TINA4_SESSION_REDIS_HOST"] || "localhost" @port = [:port] || (ENV["TINA4_SESSION_REDIS_PORT"] ? ENV["TINA4_SESSION_REDIS_PORT"].to_i : 6379) @db = [:db] || (ENV["TINA4_SESSION_REDIS_DB"] ? ENV["TINA4_SESSION_REDIS_DB"].to_i : 0) @password = [:password] || ENV["TINA4_SESSION_REDIS_PASSWORD"] @redis = build_gem_client @resp = @redis ? nil : RespClient.new(host: @host, port: @port, password: @password, db: @db) end |
Instance Method Details
#cleanup ⇒ Object
65 66 67 |
# File 'lib/tina4/session_handlers/redis_handler.rb', line 65 def cleanup # Redis handles TTL automatically end |
#destroy(session_id) ⇒ Object
60 61 62 63 |
# File 'lib/tina4/session_handlers/redis_handler.rb', line 60 def destroy(session_id) key = "#{@prefix}#{session_id}" @redis ? @redis.del(key) : @resp.del(key) end |
#read(session_id) ⇒ Object
31 32 33 34 35 36 37 38 |
# File 'lib/tina4/session_handlers/redis_handler.rb', line 31 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.
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/tina4/session_handlers/redis_handler.rb', line 49 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 |