Module: RobustServerSocket::SecureToken::Cacher

Defined in:
lib/robust_server_socket/secure_token/cacher.rb

Defined Under Namespace

Classes: RedisConnectionError

Class Method Summary collapse

Class Method Details

.atomic_validate_and_log(key, ttl, timestamp, expiration_time) ⇒ Object

Atomically validate token: check expiration and usage, then mark as used Returns: ‘ok’, ‘stale’, or ‘used’



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/robust_server_socket/secure_token/cacher.rb', line 12

def atomic_validate_and_log(key, ttl, timestamp, expiration_time)
  current_time = Time.now.utc.to_i

  redis.with do |conn|
    conn.eval(
      lua_atomic_validate,
      keys: [key],
      argv: [ttl, timestamp, expiration_time, current_time]
    )
  end
rescue ::Redis::BaseConnectionError => e
  handle_redis_error(e, 'atomic_validate_and_log')
  raise RedisConnectionError, "Failed to validate token: #{e.message}"
end

.clear_redis_pool_cache!Object

Clear cached Redis connection pool (useful for hot reloading in development)



66
67
68
# File 'lib/robust_server_socket/secure_token/cacher.rb', line 66

def clear_redis_pool_cache!
  @pool = nil
end

.get(key) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/robust_server_socket/secure_token/cacher.rb', line 41

def get(key)
  redis.with do |conn|
    conn.get(key)
  end
rescue ::Redis::BaseConnectionError => e
  handle_redis_error(e, 'get')
  nil # Fallback for reads
end

.health_checkObject



50
51
52
53
54
55
56
# File 'lib/robust_server_socket/secure_token/cacher.rb', line 50

def health_check
  redis.with do |conn|
    conn.ping == 'PONG'
  end
rescue ::Redis::BaseConnectionError
  false
end

.incr(key, ttl = nil) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/robust_server_socket/secure_token/cacher.rb', line 27

def incr(key, ttl = nil)
  ttl_value = ttl || ttl_seconds

  redis.with do |conn|
    conn.pipelined do |pipeline|
      pipeline.incrby(key, 1)
      pipeline.expire(key, ttl_value)
    end
  end
rescue ::Redis::BaseConnectionError => e
  handle_redis_error(e, 'incr')
  raise RedisConnectionError, "Failed to increment key: #{e.message}"
end

.with_redis(&block) ⇒ Object



58
59
60
61
62
63
# File 'lib/robust_server_socket/secure_token/cacher.rb', line 58

def with_redis(&block)
  redis.with(&block)
rescue ::Redis::BaseConnectionError => e
  handle_redis_error(e, 'with_redis')
  raise ::RedisConnectionError, "Redis operation failed: #{e.message}"
end