Caching
Learn about Honocord's caching system and how to use it effectively
Honocord uses a modular caching system that allows you to easily integrate different caching backends, such as in-memory, Durable Objects, MongoDB, or your own custom implementation. This guide will walk you through the basics of Honocord’s caching system, how to set it up, and best practices for using it in your implementation.
Cache Adapters
Honocord provides several built-in cache adapters:
MemoryCacheAdapter(honocord/cache/memory) - A simple in-memory cache adapter for quick caching needsDurableObjectCacheAdapter(honocord/cache/do) - A cache adapter that uses Cloudflare Durable Objects for caching on WorkersMongoCacheAdapter(honocord/cache/mongo) - A cache adapter that uses MongoDB for caching
You can also create your own custom cache adapter by extending the abstract BaseCacheAdapter class from honocord/cache.
It is important to note that you cannot use every adapter in every environment.
| Adapter | Cloudflare Workers | Node.js / Bun |
|---|---|---|
| MemoryCacheAdapter | ❌ | ✅ |
| DurableObjectCacheAdapter | ✅ | ❌ |
| MongoCacheAdapter * | ? | ✅ |
* I’m not really sure about this one, it could work on CF Workers, but I haven’t tested it yet. If you try it out, let me know how it goes!
Setting Up Caching
Every caching setup is basically the same, as you have to use the withCache method on the Honocord instance to provide a factory function that creates your cache adapter.
Only the setup before that may vary slightly based on the adapter.
Installation
All adapters ship with the honocord package as subpath exports, so there is nothing extra to install.
The only exception is the MongoDB adapter, which needs mongoose (an optional peer dependency):
Usage
Warning
This setup requires a bit more work, as you need to set up a Durable Object and bind it to your Worker.
Honocord setup
Worker setup
migrationsare required to create the Durable Object namespace - you don’t need to write any migration code, just having this here is enough for Wrangler to create the namespace for you
Why do I need to export the Durable Object class?
You might ask, why do I need to export the Durable Object class from my index.ts file?
CF Workers is a edge runtime, which means that you don’t have any in-memory state that persists across requests.
A Durable Object is a special type of object that runs on the edge and can maintain state across requests, but it needs to be defined in your code because it is a part of your Worker, not Honocord itself.
Honocord already does the heavy lifting of implementing the Durable Object and providing an adapter for it, but you still need to define it in your code so that Wrangler can create the namespace for it.
This is fairly straightforward, you just need to make sure to provide the correct MongoDB URI before using the cache adapter. You can either self-host a MongoDB instance or use a cloud provider like MongoDB Atlas.
Migrating from @honocord/cache-* packages
The separate @honocord/cache-* packages have been merged into the honocord package. Export names are unchanged, only the import paths differ:
| Old import | New import |
|---|---|
@honocord/cache-base | honocord/cache |
@honocord/cache-memory | honocord/cache/memory |
@honocord/cache-do | honocord/cache/do |
@honocord/cache-mongo | honocord/cache/mongo |
- Uninstall the old packages, e.g.
pnpm remove @honocord/cache-base @honocord/cache-memory @honocord/cache-do @honocord/cache-mongo(only the ones you installed). - Update your imports to the new paths from the table above.
- If you use the MongoDB adapter, keep
mongooseinstalled in your project.
Working with the Cache
The cache polulates automatically every time an interaction is received.
However, some things are not automatically cached due to Discord only providing partial objects in certain cases. This includes:
- Guilds - Discord only sends a partial guild object with id, features and the preferred locale; This doesn’t need to be cached.
- Resolved Channels - When a user interacts with a message component or a modal, and it has a channel select menu where resolved channels are included in the interaction data, those channels are not automatically cached. These channels are only partial and don’t include any useful information other than the ID and type.
Under the Hood
As you can see, you effectivly only have to interact with the CacheManager. The CacheManager is responsible for managing the different namespaces and providing a unified interface for accessing the cache.
When you call ctx.cache.getUser(userId), the CacheManager will delegate the call to the appropriate namespace accessor, which will then interact with the underlying cache adapter to retrieve the data.
Custom Cache Adapters
If you write a custom cache adapter, you only need to extend the abstract BaseCacheAdapter class from honocord/cache and implement the required methods.
Note
This is a very basic implementation of a Redis cache adapter. It does not handle errors, connection pooling, or other advanced features. It is only meant to serve as an example of how to implement a custom cache adapter.
A Redis cache adapter might be provided officially in the future if there is enough demand for it, but for now you can use this implementation as a starting point for your own custom cache adapter.
Then use it with Honocord just like any built-in adapter:
Fetching
On every interaction, you have a property .fetcher which is an instance of Fetcher.
The Fetcher is a utility class that provides methods for fetching data from the Discord API and automatically caching it, if not already cached.
For example, if you want to fetch a user by their ID, you can do:
This will first check if the user is already cached, and if so, it will return the cached user. If not, it will fetch the user from the Discord API, cache it, and then return it.
Important Note
Because not all endpoints are cachable (only the ones supported by the cache manager), the Fetcher has the property api which provides direct access to the Discord API without caching.
This is an instance of API from discord.js, so you can use it to make any API calls that are not supported by the cache manager. This is a high-level wrapper around the raw REST API, so it will handle things like rate limits and retries for you.