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 the Python redis-py and Node redis-npm handlers); 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
A new instance of RedisHandler.
- #read(session_id) ⇒ Object
- #write(session_id, data) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ RedisHandler
Returns a new instance of RedisHandler.
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/tina4/session_handlers/redis_handler.rb', line 12 def initialize( = {}) @prefix = [:prefix] || "tina4:session:" @ttl = [:ttl] || 86400 @host = [:host] || "localhost" @port = [:port] || 6379 @db = [:db] || 0 @password = [: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/redis_handler.rb', line 47 def cleanup # Redis handles TTL automatically end |
#destroy(session_id) ⇒ Object
42 43 44 45 |
# File 'lib/tina4/session_handlers/redis_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/redis_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/redis_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 |