Skip to main content

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

Project description


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

Create a basic cache bot:

import hikari
import hikaripersist as 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 def channels():
    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
    • MySQL
    • PostgreSQL
    • 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.

Project details


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.1.0a5.tar.gz (35.0 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.1.0a5-py3-none-any.whl (37.3 kB view details)

Uploaded Python 3

File details

Details for the file hikari_persist-0.1.0a5.tar.gz.

File metadata

  • Download URL: hikari_persist-0.1.0a5.tar.gz
  • Upload date:
  • Size: 35.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.11

File hashes

Hashes for hikari_persist-0.1.0a5.tar.gz
Algorithm Hash digest
SHA256 b94e11579f769b7f2eeced789a8d2b1c8a14f9624a58ae067438a554a598b926
MD5 dc762b74d5f72ab6bee35dc7acb0dfeb
BLAKE2b-256 bc7f3c417ca9bcc8e5946ed95863a1a28ac1d0e8738eaa5a632d4c1a416fe86a

See more details on using hashes here.

File details

Details for the file hikari_persist-0.1.0a5-py3-none-any.whl.

File metadata

File hashes

Hashes for hikari_persist-0.1.0a5-py3-none-any.whl
Algorithm Hash digest
SHA256 da876df176e0988fb2e28e03d8045a48605c030f0c53057e058080a4ff0ce04e
MD5 3deb3a5c511bbe2016a00b72952b1436
BLAKE2b-256 c10ae4cd4b8e917649253423205bc13eed73a5d90039783cb0c5fa1048eacc9f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page