Snakepit.Bridge.Session (snakepit v0.1.2)
Session data structure for centralized session management.
This struct represents a session in the centralized session store, containing all session-related data including programs, metadata, and lifecycle information.
Summary
Functions
Removes a program from the session.
Checks if a session has expired based on its TTL.
Gets metadata from the session.
Gets a program from the session.
Creates a new session with the given ID and options.
Updates session metadata.
Adds or updates a program in the session.
Updates the last_accessed timestamp to the current time.
Validates that a session struct has all required fields and valid data.
Types
Functions
Removes a program from the session.
Parameters
session
- The session to updateprogram_id
- The program identifier to remove
Returns
Updated session with the program removed.
Checks if a session has expired based on its TTL.
Parameters
session
- The session to checkcurrent_time
- Optional current time (defaults to current monotonic time)
Returns
true
if the session has expired, false
otherwise.
Gets metadata from the session.
Parameters
session
- The session to querykey
- The metadata keydefault
- Default value if key not found
Returns
The metadata value or the default.
Gets a program from the session.
Parameters
session
- The session to queryprogram_id
- The program identifier
Returns
{:ok, program_data}
if found, {:error, :not_found}
if not found.
Creates a new session with the given ID and options.
Parameters
id
- Unique session identifieropts
- Keyword list of options::ttl
- Time-to-live in seconds (default: 3600):metadata
- Initial metadata map (default: %{}):programs
- Initial programs map (default: %{})
Examples
iex> Session.new("session_123")
%Session{id: "session_123", programs: %{}, metadata: %{}, ...}
iex> Session.new("session_456", ttl: 7200, metadata: %{user_id: "user_1"})
%Session{id: "session_456", ttl: 7200, metadata: %{user_id: "user_1"}, ...}
Updates session metadata.
Parameters
session
- The session to updatekey
- The metadata keyvalue
- The metadata value
Returns
Updated session with the metadata updated.
Adds or updates a program in the session.
Parameters
session
- The session to updateprogram_id
- The program identifierprogram_data
- The program data to store
Returns
Updated session with the program added/updated.
Updates the last_accessed timestamp to the current time.
Parameters
session
- The session to touch
Returns
Updated session with current last_accessed timestamp.
Validates that a session struct has all required fields and valid data.
Parameters
session
- The session to validate
Returns
:ok
if valid, {:error, reason}
if invalid.