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):
async for guild in cache.guilds.where(id=event.guild_id).limit(1):
print(guild)
@cache.listen()
async def event_listener_2(event: hikari.Event):
async for guild in cache.guilds.where(id=event.guild_id).limit(1):
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
hikaribreaking 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file hikari_persist-0.1.0a4.tar.gz.
File metadata
- Download URL: hikari_persist-0.1.0a4.tar.gz
- Upload date:
- Size: 28.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
143d3a37921478753f6a111d2428f95fa31b4ddc3e43cbfac40f8d16e7a02475
|
|
| MD5 |
bc07aa660e18de3339ef15578f7b7c5f
|
|
| BLAKE2b-256 |
da91f9e78210d12a9cc005a899f0619d48ff1af8b73096990b05447782610c56
|
File details
Details for the file hikari_persist-0.1.0a4-py3-none-any.whl.
File metadata
- Download URL: hikari_persist-0.1.0a4-py3-none-any.whl
- Upload date:
- Size: 29.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f42c55b46362684d8f3331075a712a74459e917432745bb71c990fe511f3d9e3
|
|
| MD5 |
5be49403ed671fc5168915be8851c313
|
|
| BLAKE2b-256 |
d1aa5af2353de4da7f42195098e9ee965db6ea12a10eeb21c40c8ec8b82850d1
|