Anubis.Server.Session.Store.Redis (anubis_mcp v1.14.0)

Copy Markdown

Redis-based session store implementation.

Uses Redix for Redis communication and provides persistent session storage with automatic expiration and connection pooling.

Configuration

config :anubis_mcp, :session_store,
  adapter: Anubis.Server.Session.Store.Redis,
  redis_url: "redis://localhost:6379/0",
  pool_size: 10,
  ttl: 1_800_000, # 30 minutes in milliseconds
  namespace: "anubis:sessions",
  connection_name: :anubis_redis,
  redix_opts: []  # Optional Redix connection options

SSL/TLS Configuration

For Redis servers requiring TLS (like Upstash), pass SSL options via :redix_opts:

config :anubis_mcp, :session_store,
  adapter: Anubis.Server.Session.Store.Redis,
  redis_url: "rediss://default:password@host.upstash.io:6379",
  redix_opts: [
    ssl: true,
    socket_opts: [
      customize_hostname_check: [
        match_fun: :public_key.pkix_verify_hostname_match_fun(:https)
      ]
    ]
  ]

Features

  • Automatic session expiration using Redis TTL
  • Last-write-wins semantics for session updates
  • Connection pooling for high concurrency
  • Namespace support for multi-tenant deployments

Architecture

This module is a Supervisor whose children are the Redix connection pool plus an internal state server that answers the Anubis.Server.Session.Store behaviour calls. Because the pool lives inside the supervision tree (rather than being started from init/1), a restart of the enclosing server supervisor (which runs :one_for_all) tears the whole subtree down synchronously — releasing every registered name before the store is restarted. Restarts are therefore race-free: no {:already_started}.

Summary

Functions

Returns a specification to start this module under a supervisor.

No-op for Redis — expiry is handled natively by Redis TTLs.

Deletes any persisted state for session_id.

Lists the session ids currently held in the store's namespace.

Loads the persisted state for session_id.

Persists state for session_id, expiring after the store TTL.

Starts the Redis session store supervisor.

Merges updates into the persisted state for session_id.

Refreshes the expiry of session_id to ttl_ms milliseconds from now.

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

cleanup_expired(opts \\ [])

No-op for Redis — expiry is handled natively by Redis TTLs.

Always returns {:ok, 0}. Implements Anubis.Server.Session.Store.cleanup_expired/1.

Examples

{:ok, 0} = Anubis.Server.Session.Store.Redis.cleanup_expired()

delete(session_id, opts \\ [])

Deletes any persisted state for session_id.

Idempotent — returns :ok even when nothing was stored. Implements Anubis.Server.Session.Store.delete/2.

Examples

:ok = Anubis.Server.Session.Store.Redis.delete("sess-1")

list_active(opts \\ [])

Lists the session ids currently held in the store's namespace.

Implements Anubis.Server.Session.Store.list_active/1.

Examples

{:ok, ids} = Anubis.Server.Session.Store.Redis.list_active()

load(session_id, opts \\ [])

Loads the persisted state for session_id.

Returns {:error, :not_found} when the key is absent or has expired. Implements Anubis.Server.Session.Store.load/2.

Examples

{:ok, state} = Anubis.Server.Session.Store.Redis.load("sess-1")
{:error, :not_found} = Anubis.Server.Session.Store.Redis.load("missing")

save(session_id, state, opts \\ [])

Persists state for session_id, expiring after the store TTL.

Pass opts[:ttl] (milliseconds) to override the configured TTL for this write. Implements Anubis.Server.Session.Store.save/3.

Examples

:ok = Anubis.Server.Session.Store.Redis.save("sess-1", %{initialized: true})
:ok = Anubis.Server.Session.Store.Redis.save("sess-1", %{}, ttl: 60_000)

start_link(opts)

@spec start_link(keyword()) :: Supervisor.on_start()

Starts the Redis session store supervisor.

Supervises the Redix connection pool and the internal state server. opts is the :session_store keyword config documented in the moduledoc (:redis_url, :pool_size, :ttl, :namespace, :connection_name, :redix_opts).

Examples

{:ok, _pid} =
  Anubis.Server.Session.Store.Redis.start_link(
    redis_url: "redis://localhost:6379/0",
    namespace: "anubis:sessions"
  )

The store operations (save/3, load/2, delete/2, list_active/1, update_ttl/3, update/3, cleanup_expired/1) follow the Anubis.Server.Session.Store behaviour; see its callback docs for the request/response contract.

update(session_id, updates, opts \\ [])

Merges updates into the persisted state for session_id.

Read-modify-write with last-write-wins semantics; returns {:error, :not_found} when the session is absent. Implements Anubis.Server.Session.Store.update/3.

Examples

:ok = Anubis.Server.Session.Store.Redis.update("sess-1", %{log_level: "debug"})

update_ttl(session_id, ttl_ms, opts \\ [])

Refreshes the expiry of session_id to ttl_ms milliseconds from now.

Returns {:error, :not_found} when the session is absent. Implements Anubis.Server.Session.Store.update_ttl/3.

Examples

:ok = Anubis.Server.Session.Store.Redis.update_ttl("sess-1", 1_800_000)