Skip to content

jam.aio.modules.SessionModule

SessionModule

SessionModule(
    sessions_type: Literal["redis", "json", "custom"],
    id_factory: Callable[[], str] = lambda: str(uuid4()),
    is_session_crypt: bool = False,
    session_aes_secret: Optional[bytes] = None,
    **module_kwargs: Any
)

Bases: BaseModule

Module for session management.

Parameters:

Name Type Description Default
sessions_type Literal['redis', 'json']

Type of session storage.

required
id_factory Callable[[], str]

A callable that generates unique IDs. Defaults to a UUID factory.

lambda: str(uuid4())
is_session_crypt bool

If True, session keys will be encoded. Defaults to False.

False
session_aes_secret Optional[bytes]

AES secret for encoding session keys.

None
**module_kwargs Any

Additional keyword arguments for the session module. See

{}

Methods:

Name Description
clear

Clear all sessions by key.

create

Create a new session with the given session key and data.

delete

Delete a session by its key or ID.

get

Retrieve a session by its key or ID.

rework

Reworks a session and returns its new ID.

update

Update an existing session with new data.

clear async

clear(session_key: str) -> None

Clear all sessions by key.

Parameters:

Name Type Description Default
session_key str

The session key to clear.

required

Raises:

Type Description
SessionNotFoundError

If the session does not exist.

create async

create(session_key: str, data: dict) -> str

Create a new session with the given session key and data.

Parameters:

Name Type Description Default
session_key str

The key for the session.

required
data dict

The data to be stored in the session.

required

Returns:

Name Type Description
str str

The ID of the created session.

delete async

delete(session_id: str) -> None

Delete a session by its key or ID.

Parameters:

Name Type Description Default
session_id str

The ID of the session to delete.

required

Raises:

Type Description
SessionNotFoundError

If the session does not exist.

get async

get(session_id: str) -> Optional[dict]

Retrieve a session by its key or ID.

Parameters:

Name Type Description Default
session_id str

The ID of the session to retrieve.

required

Returns:

Type Description
Optional[dict]

dict | None: The data stored in the session.

Raises:

Type Description
SessionNotFoundError

If the session does not exist.

rework async

rework(session_id: str) -> str

Reworks a session and returns its new ID.

Parameters:

Name Type Description Default
session_id str

The ID of the session to rework.

required

Returns:

Name Type Description
str str

The new ID of the reworked session.

Raises:

Type Description
SessionNotFoundError

If the session does not exist.

update async

update(session_id: str, data: dict) -> None

Update an existing session with new data.

Parameters:

Name Type Description Default
session_id str

The ID of the session to update.

required
data dict

The new data to be stored in the session.

required

Raises:

Type Description
SessionNotFoundError

If the session does not exist.

FILE PATH: jam/aio/modules.py