Skip to main content


A lightweight and modular persistent cache library for hikari-based Discord bots


Overview

hikari-persist is a persistent caching library for hikari that provides a familiar API with local data.

It is designed to be:

  • Simple to use
  • Fully asynchronous
  • Similar to hikari's REST functionality

Contents

Features

  • Async-first, awaitable API
  • Strong typing and documentation throughout (Pylance/MyPy friendly)
  • Designed specifically for hikari's async model
  • Minimal overhead and predictable behavior
  • Fully modular with custom database backends
  • Automatic database migration
  • Rulesets to control what data is cached

Purpose

Unlike hikari's built-in, in-memory cache, hikari-persist:

  • Survives restarts and crashes
  • Scales across large guild counts
  • Allows database-level inspection and analytics
  • Supports pluggable storage backends

Installation

pip install hikari-persist

Quick Start

Depending on which database backend you'd like to use, you need to install their dependencies:

  • persist.SQLiteBackend: Install using hikari-persist[sqlite] or aiosqlite.

Create a basic cache bot:

import hikari
import persist

bot = hikari.GatewayBot("TOKEN")
cache = persist.Cache(bot, persist.SQLiteBackend("cache.db"))

bot.run()

The cache automatically gathers data as dispatched by the GatewayBot and populates the persistent cache. To access all data cached, the Cache object exposes public-facing query properties.

Examples of iterating through the cache looks like this:

async for channel in cache.channels.all():
    ... # Lazily iterate through all cached channels

async for channel in cache.channels.where(name="general", type=hikari.ChannelType.GUILD_TEXT):
    print(type(channel)) # >>> <class 'hikari.GuildTextChannel'>

The .all() query method returns an async iterator over the cache. The .where() query method returns a filtered async iterator over the cache.

The iterator itself, CacheIterator, exposes methods like filter, map, limit, chunk, etc. for convenience. Each object returned by the cache is a native hikari object, like GuildTextChannel, Role, Member, etc.

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.

@cache.listen() # <- note `cache` not `bot`
async def event_listener(event: hikari.Event):
    ...

This approach works well for most operations, however, due to the nature of asynchronous, batched databases, the cache is not guaranteed to have the changes complete by the time the event is dispatched. To ensure the cache is properly changed before you see the event, pass confirm=True into the decorator.

@cache.listen(confirm=True) # <- confirm=True
async def event_listener(event: hikari.Event):
    ...

The difference between the two is this:

@cache.listen()
async def event_listener_1(event: hikari.Event):
    guild = await cache.guilds.get(id=event.guild_id)
    print(guild)

@cache.listen(confirm=True)
async def event_listener_2(event: hikari.Event):
    guild = await cache.guilds.get(id=event.guild_id)
    print(guild)
>>> None (or sometimes `hikari.Guild`)
>>> `hikari.Guild`

The confirm=True ensures the cache is up to date before dispatching the event. Otherwise, it's not guaranteed.

Most implementations will not need to worry about this, but it's here just in case it's necessary. The confirmation logic does introduce slight latency to the event dispatch, but it's negligible unless you worry about real-time performance.

TL;DR:

  • confirm=False (or omitted): Fire-and-forget (default, very fast)
  • confirm=True: Waits for database write to complete (slight latency)

Cache/Database Stability

Before 1.0.0, the database schema may change between versions. If this occurs, delete your cache database and allow it to rebuild.

Database migration is implemented, but early migration is cumbersome when getting the API stabilized.

Implemented Features

  • Basic objects (channels, guilds, members, etc.)
  • Advanced lookups (filter, map, limit, etc.)
  • Database backends:
    • SQLite
    • PostgreSQL
    • MySQL
    • Redis
    • JSON

Documentation

Full documentation is available at: https://hikari-persist.wildevstudios.net/

Library Lifecycle

See https://hikari-persist.wildevstudios.net/en/latest/pages/lifecycle for the full list of deprecated and experimental features.

Help and Contact

Feel free to join the hikari Discord server under the #persist channel for assistance.

Versioning & Stability Policy

hikari-persist follows Semantic Versioning with a clear and practical stability model designed to balance rapid development with reliability.

Discord API Changes

Though hikari-persist follows Semantic Versioning, there is one exception to the versioning requirements/standard:

Breaking changes caused by Discord API modifications, such as removed or renamed fields, changed behavior, or deprecated endpoints, will result in a minor version bump rather than a major one. This is because such changes are outside the library's control and do not reflect a design decision made by this project.

If you are pinning to a specific version and Discord introduces a breaking API change, upgrading to the new minor release may require changes to your code. The changelog will always clearly identify when a bump is driven by a Discord API change.

hikari-persist pins to specific tested versions of hikari. Any breaking changes in hikari may affect this library's behavior, and a correlating update will be released for each new hikari version. It is recommended to pin your dependency to a specific minor version but allow patch updates, allowing bug fixes while avoiding potentially breaking minor updates, something like hikari-persist~=1.2.0.

Bump Reason
Major (X.0.0) Breaking changes to the library's own API
Minor (x.Y.0) New features; breaking changes forced by Discord or hikari
Patch (x.y.Z) Bug fixes; internal improvements

Patch Releases (x.y.Z)

  • Bug fixes and internal improvements only
  • No breaking changes
  • Always considered stable
  • No alpha (a) or beta (b) suffixes

Patch releases are safe to upgrade to without code changes.

Minor Releases (x.Y.0)

  • Introduce new features, subsystems, or configuration options
  • Existing public APIs generally preserved, but behavior may expand
  • May include short-lived alpha/beta pre-releases before stabilization
  • Discord API or hikari breaking changes

Pre-Releases (a/b)

  • Used only for new minor/major versions
  • Intended for developers who want early access to new features/versions
  • Not recommended for production unless you are testing upcoming functionality

Pre-releases exist to gather feedback and catch issues early. Once stabilized, the same version is released as a stable minor.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hikari_persist-0.2.0.tar.gz (36.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

hikari_persist-0.2.0-py3-none-any.whl (41.3 kB view details)

Uploaded Python 3

File details

Details for the file hikari_persist-0.2.0.tar.gz.

File metadata

  • Download URL: hikari_persist-0.2.0.tar.gz
  • Upload date:
  • Size: 36.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.10.11

File hashes

Hashes for hikari_persist-0.2.0.tar.gz
Algorithm Hash digest
SHA256 360c726511d4e1d502d7893b3063ba20cade2f895667b53926b4dc6e6e180579
MD5 a17dbc055079f01e2f9dd73a379cf4aa
BLAKE2b-256 ef658c229e1577814df6163836bcecf7069ae0f653ace3da1ea4e0b6ed7363b2

See more details on using hashes here.

File details

Details for the file hikari_persist-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: hikari_persist-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 41.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.10.11

File hashes

Hashes for hikari_persist-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b66658ce648c31d271c95492f9056e213c3798b77f9e0a2809e9deee9b105df9
MD5 0c9e6e06a7030d4711454c1aa63ec82b
BLAKE2b-256 d1433afed3c22c51213a8d840ad93d2d785e4261bb5a6b71dac424d5ddaaca4e

See more details on using hashes here.

Release history Release notifications | RSS feed

0.3.0

2 files

This release

0.2.0 This release

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page