Skip to content

jam.aio.sessions.redis

redis

Classes:

Name Description
RedisSessions

Async Redis session management module.

RedisSessions

RedisSessions(
    redis_uri: str | Redis = "redis://localhost:6379/0",
    redis_sessions_key: str = "sessions",
    default_ttl: int | None = 3600,
    is_session_crypt: bool = False,
    session_aes_secret: bytes | str | None = getenv(
        "JAM_SESSION_AES_SECRET", None
    ),
    id_factory: Callable[[], str] = lambda: str(uuid4()),
    serializer: BaseEncoder
    | type[BaseEncoder] = JsonEncoder,
    logger: BaseLogger | None = None,
)

Bases: BaseAsyncSessionModule

Async Redis session management module.

Parameters:

Name Type Description Default
redis_uri str | Redis

The URI for the Redis server or Redis instance.

'redis://localhost:6379/0'
redis_sessions_key str

The key under which sessions are stored in Redis.

'sessions'
default_ttl Optional[int]

Default time-to-live for sessions in seconds. Defaults to 3600 seconds (1 hour).

3600
is_session_crypt bool

If True, session keys will be encoded.

False
session_aes_secret Optional[bytes]

AES secret for encoding session keys. Required if is_session_key_crypt is True.

getenv('JAM_SESSION_AES_SECRET', None)
id_factory Callable[[], str]

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

lambda: str(uuid4())
serializer Union[BaseEncoder, type[BaseEncoder]]

JSON encoder/decoder. Defaults to JsonEncoder.

JsonEncoder
logger Optional[BaseLogger]

Logger instance. Defaults to None.

None

Methods:

Name Description
clear

Clear all sessions for a given session key.

create

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

delete

Delete a session by its ID.

get

Retrieve a session by its key or ID.

rework

Rework a session and return its new ID.

update

Update an existing session with new data.

clear async

clear(session_key: str) -> None

Clear all sessions for a given session key.

Parameters:

Name Type Description Default
session_key str

The session key to clear.

required

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 unique ID of the created session.

delete async

delete(session_id: str) -> None

Delete a session by its ID.

Parameters:

Name Type Description Default
session_id str

The session ID.

required

get async

get(session_id: str) -> dict | None

Retrieve a session by its key or ID.

Parameters:

Name Type Description Default
session_id str

The session key or ID.

required

Returns:

Type Description
dict | None

dict | None: The session data if found, otherwise None.

rework async

rework(session_id: str) -> str

Rework a session and return its new ID.

Parameters:

Name Type Description Default
session_id str

The ID of the session to rework.

required

Raises:

Type Description
JamSessionNotFound

If the session with the given ID does not exist.

Returns:

Name Type Description
str str

The new session ID.

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
JamSessionNotFound

If the session with the given ID does not exist.