ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

Redis Lua Session Expiration Manager

Redis Lua

Goal -- WPM

Ready
Exercise Algorithm Area
1local function setSession(session_key, session_data, ttl_seconds)
2-- Sets a session with data and a time-to-live.
3-- Args:
4-- session_key: The unique key for the session.
5-- session_data: The data associated with the session (as a string).
6-- ttl_seconds: The expiration time in seconds.
7-- Returns:
8-- 'OK' on success, or an error reply.
9
10if not session_key or not session_data or not ttl_seconds then
11return redis.error_reply('Invalid arguments: session_key, session_data, and ttl_seconds are required.')
12end
13
14-- Use SET with EX option for atomic set and expire.
15local result = redis.call('SET', session_key, session_data, 'EX', ttl_seconds)
16
17if result == 'OK' then
18return 'OK'
19else
20return redis.error_reply('Failed to set session.')
21end
22end
23
24local function isSessionValid(session_key)
25-- Checks if a session is still valid (exists and has not expired).
26-- Args:
27-- session_key: The unique key for the session.
28-- Returns:
29-- 1 if the session is valid, 0 if expired or not found, or an error reply.
30
31if not session_key then
32return redis.error_reply('Invalid arguments: session_key is required.')
33end
34
35-- GET will return nil if the key does not exist or has expired.
36local session_data = redis.call('GET', session_key)
37
38if session_data == nil then
39return 0
40else
41return 1
42end
43end
Algorithm description viewbox

Redis Lua Session Expiration Manager

Algorithm description:

This script provides basic session management functionality using Redis. It allows setting session data with an automatic expiration time and checking if a session is still active. This is fundamental for applications that need to maintain user state or temporary data, ensuring that stale sessions are automatically removed.

Algorithm explanation:

The `setSession` function uses `redis.call('SET', session_key, session_data, 'EX', ttl_seconds)` to atomically set the session data and its expiration time. The `EX` option ensures that the key will be automatically deleted after `ttl_seconds`. The `isSessionValid` function uses `redis.call('GET', session_key)`. If `GET` returns `nil`, it means the key either never existed or has expired and been deleted by Redis. In this case, the session is considered invalid (returns 0). Otherwise, the session is considered valid (returns 1). Both operations are O(1) in time complexity as they involve single Redis commands. Space complexity is O(S) where S is the size of the session data.

Pseudocode:

FUNCTION setSession(session_key, session_data, ttl_seconds):
  result = Redis.SET(session_key, session_data, 'EX', ttl_seconds)
  IF result IS 'OK' THEN
    RETURN 'OK'
  ELSE
    RETURN error 'Failed to set session'
  END IF
END FUNCTION

FUNCTION isSessionValid(session_key):
  session_data = Redis.GET(session_key)
  IF session_data IS null THEN
    RETURN 0
  ELSE
    RETURN 1
  END IF
END FUNCTION