Class: Tina4::SessionHandlers::ValkeyHandler

Inherits:
Object
  • Object
show all
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

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(options = {})
  @prefix = options[:prefix] || ENV["TINA4_SESSION_VALKEY_PREFIX"] || "tina4:session:"
  @ttl = options[:ttl] || (ENV["TINA4_SESSION_VALKEY_TTL"] ? ENV["TINA4_SESSION_VALKEY_TTL"].to_i : 86400)
  @host = options[:host] || ENV["TINA4_SESSION_VALKEY_HOST"] || "localhost"
  @port = options[:port] || (ENV["TINA4_SESSION_VALKEY_PORT"] ? ENV["TINA4_SESSION_VALKEY_PORT"].to_i : 6379)
  @db = options[:db] || (ENV["TINA4_SESSION_VALKEY_DB"] ? ENV["TINA4_SESSION_VALKEY_DB"].to_i : 0)
  @password = options[: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

#cleanupObject



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