Skip to content

hikaripersist.cache

Cache

Cache(
    bot: hikari.GatewayBot,
    backend: Backend,
    *,
    rule: Rule | None = None
)

Persistent cache for hikari-based Discord bots.

Create a new persistent cache.

PARAMETER DESCRIPTION
bot

The bot to interface this cache with.

TYPE: hikari.GatewayBot

backend

The database backend to use with this cache.

TYPE: Backend

rule

If provided, a ruleset regarding what is cached.

TYPE: Rule | None DEFAULT: None

RAISES DESCRIPTION
RuntimeError

If an instance of Cache already exists.

TypeError
  • If bot is not hikari.GatewayBot.
  • If backend is not Backend.
  • If rule is provided and is not Rule.

bot property

The bot interfaced with this cache.

channels property

channels: ChannelQuery

Interact with cache channels.

guilds property

guilds: GuildQuery

Interact with cache guilds.

members property

members: MemberQuery

Interact with cache members.

roles property

roles: RoleQuery

Interact with cache roles.

backup async

backup(path: Path | str) -> None

Snapshot the current cache backend to a file.

PARAMETER DESCRIPTION
path

The path to the file to write.

TYPE: Path | str

RAISES DESCRIPTION
TypeError

If path is not Path or str.

clear async

clear(
    *,
    channels: bool = False,
    guilds: bool = False,
    members: bool = False,
    roles: bool = False
) -> None

Clear the cache of specific data.

PARAMETER DESCRIPTION
channels

If cache channel data should be cleared.

TYPE: bool DEFAULT: False

guilds

If cache guild data should be cleared.

TYPE: bool DEFAULT: False

members

If cache member data should be cleared.

TYPE: bool DEFAULT: False

roles

If cache role data should be cleared.

TYPE: bool DEFAULT: False

RAISES DESCRIPTION
TypeError

If any parameter is not bool.

listen

listen(
    event: type[EventT] | None = None,
    *,
    confirm: bool = False
) -> Callable[
    [Callable[[EventT], Awaitable[None]]],
    Callable[[EventT], Awaitable[None]],
]

Listen for an event and add this method as a callback.

PARAMETER DESCRIPTION
event

The event object to listen for, if provided, otherwise extracted from callback's first parameter type.

TYPE: type[EventT] | None DEFAULT: None

confirm

If True, not dispatched until the cache has confirmed the change. If False, dispatched as soon as the cache receives the event.

TYPE: bool DEFAULT: False

Note

To ensure that the cache sees all event data before being handled, the cache acts as a middle-man in event dispatching. Instead of using @bot.listen(), use @cache.listen() and the cache will dispatch each event normally after it's complete.

restore async

restore(path: Path | str) -> None

Restore a cache backend from a file.

PARAMETER DESCRIPTION
path

The path to the file to read.

TYPE: Path | str

RAISES DESCRIPTION
TypeError

If path is not Path or str.

subscribe

subscribe(
    event: type[EventT],
    callback: Callable[[EventT], Awaitable[None]],
    *,
    confirm: bool = False
) -> None

Subscribe to an event with a handler callback.

PARAMETER DESCRIPTION
event

The event object to subscribe to.

TYPE: type[EventT]

callback

The handler callback method.

TYPE: Callable[[EventT], Awaitable[None]]

confirm

If True, not dispatched until the cache has confirmed the change. If False, dispatched as soon as the cache receives the event.

TYPE: bool DEFAULT: False

unsubscribe

unsubscribe(
    event: type[EventT],
    callback: Callable[[EventT], Awaitable[None]],
) -> None

Unsubscribe a handler callback from an event.

PARAMETER DESCRIPTION
event

The event object to unsubscribe from.

TYPE: type[EventT]

callback

The handler callback method to unsubscribe.

TYPE: Callable[[EventT], Awaitable[None]]