Skip to content

sake.redis#

Redis based implementations of Sake's clients.

DEFAULT_EXPIRE: typing.Final[int] = 3600000 module-attribute #

The default expire time (in milliseconds) used for expiring resources of 60 minutes.

DEFAULT_FAST_EXPIRE: typing.Final[int] = 300000 module-attribute #

The default expire time (in milliseconds) used for expiring resources quickly of 5 minutes.

DEFAULT_INVITE_EXPIRE: typing.Final[int] = 2592000000 module-attribute #

A special case month long default expire time for invite entries without a set "expire_at".

DEFAULT_SLOW_EXPIRE: typing.Final[int] = 604800 module-attribute #

The default expire time (in milliseconds) used for gateway-event deleted resources (1 week).

EmojiCache #

Bases: _Reference, sake_abc.RefEmojiCache

Redis implementation of sake.abc.EmojiCache.

GuildCache #

Bases: ResourceClient, sake_abc.GuildCache

Redis implementation of sake.abc.GuildCache.

GuildChannelCache #

Bases: _Reference, sake_abc.RefGuildChannelCache

Redis implementation of sake.abc.RefGuildChannelCache.

InviteCache #

Bases: ResourceClient, sake_abc.InviteCache

Redis implementation of sake.abc.InviteCache.

with_invite_expire(expire) #

Set the default expire time for invite entries added with this client.

PARAMETER DESCRIPTION
expire

The default expire time to add for invites in this cache or None to set back to the default behaviour. This may either be the number of seconds as an int or float (where millisecond precision is supported) or a timedelta.

TYPE: typing.Optional[_internal.ExpireT]

RETURNS DESCRIPTION
_ResourceT

The client this is being called on to enable chained calls.

MemberCache #

Bases: ResourceClient, sake_abc.MemberCache

Redis implementation of sake.abc.MemberCache.

MessageCache #

Bases: ResourceClient, sake_abc.MessageCache

Redis implementation of sake.abc.MessageCache.

with_message_expire(expire) #

Set the default expire time for message entries added with this client.

PARAMETER DESCRIPTION
expire

The default expire time to add for messages in this cache or None to set back to the default behaviour. This may either be the number of seconds as an int or float (where millisecond precision is supported) or a timedelta.

TYPE: typing.Optional[_internal.ExpireT]

RETURNS DESCRIPTION
_ResourceT

The client this is being called on to enable chained calls.

PresenceCache #

Bases: ResourceClient, sake_abc.PresenceCache

Redis implementation of sake.abc.PresenceCache.

RedisCache #

Bases: GuildCache, EmojiCache, GuildChannelCache, InviteCache, MemberCache, MessageCache, PresenceCache, RoleCache, UserCache, VoiceStateCache, sake_abc.Cache

A Redis implementation of all the defined cache resources.

ResourceClient #

Bases: sake_abc.Resource, abc.ABC

A base client which all resources in this implementation will implement.

Note

This cannot be initialised by itself and is useless alone.

__init__(address, app, event_manager=None, *, config=None, default_expire=DEFAULT_SLOW_EXPIRE, event_managed=False, password=None, max_connections_per_db=5, dumps=lambda obj: json.dumps(obj).encode(), loads=json.loads) #

Initialise a resource client.

PARAMETER DESCRIPTION
app

The Hikari client all the models returned by this client should be bound to.

TYPE: traits.RESTAware

address

The address to use to connect to the Redis backend server this resource is linked to.

E.g: - "redis://[[username]:[password]]@localhost:6379" - "rediss://[[username]:[password]]@localhost:6379" - "unix://[[username]:[password]]@/path/to/socket.sock"

Three URL schemes are supported: - redis://: creates a TCP socket connection. See more at: https://www.iana.org/assignments/uri-schemes/prov/redis - rediss://: creates a SSL wrapped TCP socket connection. See more at: https://www.iana.org/assignments/uri-schemes/prov/rediss - unix://: creates a Unix Domain Socket connection.

TYPE: str

event_manager

The event manager to bind this resource client to.

If provided then this client will automatically manage resources based on received gateway events.

TYPE: typing.Optional[hikari.api.EventManager] DEFAULT: None

event_managed

Whether the client should be started and stopped based on the attached event_manager's lifetime events.

TYPE: bool DEFAULT: False

password

The password to use to connect to the backend Redis server.

TYPE: typing.Optional[str] DEFAULT: None

RAISES DESCRIPTION
ValueError

When event_managed is True and event_manager wasn't passed.

add_to_tanjun(client, /, *, trust_get_for=None, tanjun_managed=False) #

Add this Redis client to a Tanjun client.

This method will register type dependencies for the resources implemented by it (including tanjun.dependencies.async_cache compatible adapters which will allow Tanjun extensions and standard utility such as converters to be aware of this cache).

The type dependencies this will register depend on which resources are implemented but are as follows for each standard resource implementation:

PARAMETER DESCRIPTION
client

The Tanjun client to add this client to.

TYPE: tanjun.abc.Client

trust_get_for

A collection of resource types which the tanjun.dependencies.async_cache adapter "get" methods should raise tanjun.dependencies.async_cache.EntryNotFound if the entry isn't found rather than just tanjun.dependencies.async_cache.CacheMissError.

If not passed then this will default to the following resources:

TYPE: typing.Optional[typing.Collection[typing.Type[sake_abc.Resource]]] DEFAULT: None

tanjun_managed

Whether the client should be started and stopped based on the tanjun client's lifecycle.

This is useful if the client isn't being event managed.

TYPE: bool DEFAULT: False

RAISES DESCRIPTION
RuntimeError

If this is called in an environment without Tanjun.

all_indexes() #

Get a set of all the Redis client indexes this is using.

Note

This accounts for index overrides.

RETURNS DESCRIPTION
typing.MutableSet[ResourceIndex | int]

A set of all the Redis client indexes this is using.

all_intents() classmethod #

The intents required for a client to be sufficient event managed.

If not all these intents are present in the linked event manager then this client won't be able to reliably fill and manage the linked redis database(s).

app() property #

The Hikari client this resource client is tied to.

This is used to build models with a app attribute.

config() property #

This client's settings.

default_expire() property #

The default expire time used for fields with no actual lifetime.

If this is None then these cases will have no set expire after.

dump(data) #

Serialize a dict object representation into the form to be stored.

PARAMETER DESCRIPTION
data

The dict object to serialize.

TYPE: _ObjectT

RETURNS DESCRIPTION
bytes

The object serialized as bytes.

event_manager() property #

The event manager this resource client is using for managing state.

get_connection(resource) #

Get the connection for a specific resource.

PARAMETER DESCRIPTION
resource

The index of the resource to get a connection for.

TYPE: ResourceIndex

RETURNS DESCRIPTION
aioredis.Redis

The connection instance for the specified resource.

RAISES DESCRIPTION
TypeError

When this method is called on a closed client.

ValueError

When you pass an invalid resource for the client.

get_connection_status(resource) async #

Get the status of the internal connection for a specific resource.

PARAMETER DESCRIPTION
resource

The index of the resource to get the status for.

TYPE: ResourceIndex

RETURNS DESCRIPTION
bool

Whether the client has an active connection for the specified resource.

get_index_override(index) #

Get the override set for an index.

PARAMETER DESCRIPTION
index

The index to get the override for.

TYPE: ResourceIndex

RETURNS DESCRIPTION
int | None

The found override if set, else None.

index() classmethod abstractmethod #

The index for the resource which this class is linked to.

Note

This should be called on specific base classes and will not be accurate after inheritance.

Warning

This doesn't account for overrides.

RETURNS DESCRIPTION
typing.Sequence[ResourceIndex]

The index of the resource this class is linked to.

intents() classmethod abstractmethod #

The intents the resource requires to function properly.

Note

This should be called on specific base classes and will not be accurate after inheritance.

RETURNS DESCRIPTION
hikari.intents.Intents

The intents the resource requires to function properly.

load(data) #

Deserialize a bytes representation to a dict object.

PARAMETER DESCRIPTION
data

The bytes representation from the database to a dict object.

TYPE: bytes

RETURNS DESCRIPTION
dict[str, typing.Any]

The deserialized dict object.

with_index_override(index, /, *, override=None) #

Add an index override.

PARAMETER DESCRIPTION
index

The index to override.

TYPE: ResourceIndex

override

The override to set.

If this is left at None then any previous override is unset. This will decide which Redis database is targeted for a resource.

TYPE: typing.Optional[int] DEFAULT: None

ResourceIndex #

Bases: enum.IntEnum

An enum of the indexes used to map cache resources to their redis databases.

RoleCache #

Bases: _Reference, sake_abc.RoleCache

Redis implementation of sake.abc.RoleCache.

UserCache #

Bases: _MeCache, sake_abc.UserCache

Redis implementation of sake.abc.UserCache.

with_user_expire(expire) #

Set the default expire time for user entries added with this client.

PARAMETER DESCRIPTION
expire

The default expire time to add for users in this cache or None to set back to the default behaviour. This may either be the number of seconds as an int or float (where millisecond precision is supported) or a timedelta.

TYPE: typing.Optional[_internal.ExpireT]

RETURNS DESCRIPTION
_ResourceT

The client this is being called on to enable chained calls.

VoiceStateCache #

Bases: _Reference, sake_abc.VoiceStateCache

Redis implementation of sake.abc.VoiceStateCache.