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¶
Connects to the configured session storage backend and exposes a uniform interface for get, set, update, delete, and clear operations. |
Functions¶
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.
- 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.
- 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
- 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.