Main Usage#

This is the most general use-case for Mirex

class mirex.Mirex(*, redis_instance: Redis, namespace, connection_state)#
__init__(*, redis_instance: Redis, namespace, connection_state)#
Parameters:
  • redis_instance (aioredis.Redis) – Your redis instance

  • namespace – The variable namespace for your library. This will generally be import disnake - namespace=disnake

  • connection_state – Your bots connection state. bot._connection

add_to_cache(item: dict | Any, ttl: timedelta | None = None) None#

Adds an item to the cache.

Parameters:
  • item (Union[dict, Any]) – The entry to be cached. This can either be the converted dictionary or class to convert.

  • ttl (Optional[datetime.timedelta]) – How many seconds this item should live in the cache for before eviction.

Raises:

ValueError – The provided class instance doesn’t have a conversion method yet

async aget_class(key, mapping: Type[T]) T | None#

Internal helper method that all aget_* methods use.

Parameters:
  • key (str) – The key to fetch

  • mapping – The class we want to return an instance of

Returns:

The class, if data was found in redis

Return type:

Optional[T]

async aget_guild(guild_id: int) Any | None#

Return a Guild instance from cache.

Parameters:

guild_id (int) – The guilds id

Returns:

The guild if found.

Return type:

Optional[Guild]

async consume_eviction()#

Handle cache eviction.

async consume_queue()#

Handle cache addition.

inject_hooks()#

Inject Mirex into your discord library so the cache is automatically populated.

This does remove your libraries built in caching so you will need to use Mirex for all cache hits after this call.

remove_from_cache(key: str | Any) None#

Evict something from the cache.

Parameters:

key (Union[str, Any]) –

The key to remove from cache.

Keys are structured as the class name in all capitals and the ID of the object seperated by a :

For example “GUILD:808030843078836254”