# `Snakepit.Bridge.SessionStore`
[🔗](https://github.com/nshkrdotcom/snakepit/blob/v0.13.0/lib/snakepit/bridge/session_store.ex#L1)

Centralized session store using ETS for high-performance session management.

This GenServer manages a centralized ETS table for storing session data,
providing CRUD operations, TTL-based expiration, and automatic cleanup.
The store is designed for high concurrency with optimized ETS settings.

# `child_spec`

Returns a specification to start this module under a supervisor.

See `Supervisor`.

# `cleanup_expired_sessions`

```elixir
@spec cleanup_expired_sessions(GenServer.server()) :: non_neg_integer()
```

Manually triggers cleanup of expired sessions.

## Returns

The number of sessions that were cleaned up.

# `create_session`

```elixir
@spec create_session(
  String.t(),
  keyword()
) :: {:ok, Snakepit.Bridge.Session.t()} | {:error, term()}
```

Creates a new session with the given ID and options.

## Parameters

- `session_id` - Unique session identifier
- `opts` - Keyword list of options passed to Session.new/2

## Returns

`{:ok, session}` if successful, `{:error, reason}` if failed.

## Examples

    {:ok, session} = SessionStore.create_session("session_123")
    {:ok, session} = SessionStore.create_session("session_456", ttl: 7200)

# `create_session`

```elixir
@spec create_session(GenServer.server(), String.t(), keyword()) ::
  {:ok, Snakepit.Bridge.Session.t()} | {:error, term()}
```

# `delete_session`

```elixir
@spec delete_session(GenServer.server(), String.t()) :: :ok
```

Deletes a session by ID.

## Parameters

- `session_id` - The session identifier

## Returns

`:ok` always (idempotent operation).

# `get_session`

```elixir
@spec get_session(GenServer.server(), String.t()) ::
  {:ok, Snakepit.Bridge.Session.t()} | {:error, :not_found}
```

Gets a session by ID, automatically updating the last_accessed timestamp.

## Parameters

- `session_id` - The session identifier

## Returns

`{:ok, session}` if found, `{:error, :not_found}` if not found.

# `get_stats`

```elixir
@spec get_stats(GenServer.server()) :: map()
```

Gets statistics about the session store.

## Returns

A map containing various statistics about the session store.

# `list_sessions`

```elixir
@spec list_sessions(GenServer.server()) :: [String.t()]
```

Lists all active session IDs.

## Returns

A list of all active session IDs.

# `session_exists?`

```elixir
@spec session_exists?(GenServer.server(), String.t()) :: boolean()
```

Checks if a session exists.

## Parameters

- `session_id` - The session identifier

## Returns

`true` if the session exists, `false` otherwise.

# `start_link`

```elixir
@spec start_link(keyword()) :: GenServer.on_start()
```

Starts the SessionStore GenServer.

## Options

- `:name` - The name to register the GenServer (default: __MODULE__)
- `:table_name` - The ETS table name (default: :snakepit_sessions)
- `:cleanup_interval` - Cleanup interval in milliseconds (default: 60_000)
- `:default_ttl` - Default TTL for sessions in seconds (default: 3600)

# `store_worker_session`

```elixir
@spec store_worker_session(String.t(), term()) :: :ok | {:error, term()}
```

Stores worker-session affinity mapping.

# `store_worker_session`

```elixir
@spec store_worker_session(GenServer.server(), String.t(), term()) ::
  :ok | {:error, term()}
```

# `update_session`

```elixir
@spec update_session(GenServer.server(), String.t(), (Snakepit.Bridge.Session.t() -&gt;
                                                  Snakepit.Bridge.Session.t())) ::
  {:ok, Snakepit.Bridge.Session.t()} | {:error, term()}
```

Updates a session using the provided update function.

The update function receives the current session and should return
the updated session. The operation is atomic.

## Parameters

- `session_id` - The session identifier
- `update_fn` - Function that takes a session and returns an updated session

## Returns

`{:ok, updated_session}` if successful, `{:error, reason}` if failed.

## Examples

    {:ok, session} = SessionStore.update_session("session_123", fn session ->
      Map.put(session, :data, %{key: "value"})
    end)

---

*Consult [api-reference.md](api-reference.md) for complete listing*
