duck.http.session.connector

Module containing SessionConnector class which can be used to connect to session storage to perform operations like get, set, update, delete, clear, etc.

Module Contents

Classes

SessionStorageConnector

Connects to the configured session storage backend and exposes a uniform interface for get, set, update, delete, and clear operations.

Functions

get_session_storage_connector

Returns the session storage connector object.

API

class duck.http.session.connector.SessionStorageConnector(session_storage_cls: type[duck.utils.caching.CacheBase])[source]

Connects to the configured session storage backend and exposes a uniform interface for get, set, update, delete, and clear operations.

A write-through in-memory cache sits in front of the real storage backend to reduce latency on repeated reads. The cache layer is bypassed entirely when the backend is itself an InMemoryCache.

Initialization

Initializes the connector with the given storage backend class.

Initialization only runs once — subsequent calls with the same class are no-ops. Passing a different class after initialization raises an error to prevent silent misconfiguration.

Parameters:

session_storage_cls – The storage backend class to instantiate.

Raises:

SessionStorageConnectorError – If already initialized with a different class.

CACHED_SESSIONS

‘InMemoryCache(…)’

Write-through in-memory cache that fronts the real session storage backend.

__new__(session_storage_cls: type[duck.utils.caching.CacheBase])[source]
clear_all_sessions() None[source]

Clears all sessions from the backend and flushes the in-memory cache.

close() None[source]

Closes the session storage backend connection.

delete_session(session_id: str) None[source]

Deletes a session from both the backend and the in-memory cache.

Parameters:

session_id – The session identifier to remove.

static generate_session_id() str[source]

Returns a randomly generated session ID.

get_session(session_id: str) dict | None[source]

Retrieves session data, consulting the in-memory cache first.

Parameters:

session_id – The session identifier.

Returns:

The session data dict, or None if the session does not exist.

initialized

False

instance

None

save() None[source]

Persists current session state to the backend storage.

set_session(session_id: str, data: dict, expiry: int | float | None = None) None[source]

Writes session data to the backend and updates the in-memory cache.

Parameters:
  • session_id – The session identifier.

  • data – The session data to store.

  • expiry – Optional TTL in seconds. Omitted from the call if not provided.

update_session(session_id: str, data: dict) None[source]

Merges new data into an existing session, preserving existing keys.

Parameters:
  • session_id – The session identifier.

  • data – Partial data to merge into the current session.

property using_memory_backend: bool

Returns True when the backend is itself an InMemoryCache.

When True, the write-through cache layer is bypassed to avoid double-storing the same data in two separate InMemoryCache instances.

exception duck.http.session.connector.SessionStorageConnectorError[source]

Bases: Exception

Raised when errors related to the session storage connector arise.

Initialization

Initialize self. See help(type(self)) for accurate signature.

duck.http.session.connector.get_session_storage_connector()[source]

Returns the session storage connector object.