CacheManager
CacheManager provides a structured, namespace-aware layer on top of a BaseCacheAdapter. It is set on the Hono context by Honocord after calling withCache() and is accessible in middleware and handlers as c.var.cache.
const cache = c.var.cache; // CacheManager | nullConstructor
Section titled “Constructor”new CacheManager(adapter: BaseCacheAdapter, defaultTtlMs?: number)adapter— the cache backend to use.defaultTtlMs— fallback TTL in milliseconds for allset/msetcalls that do not supply their own TTL. Defaults to300_000(5 minutes).
Public properties
Section titled “Public properties”All namespace accessors share a common interface (see below). Members are the exception — they are guild-scoped and require a guildId on every call.
channels: NamespaceAccessor<CachedChannel> (readonly)
Section titled “channels: NamespaceAccessor<CachedChannel> (readonly)”Cached channel objects. Note: channels resolved from raw interaction payloads are not populated here automatically — Discord only provides partial objects (id + type) in those cases.
getDMChannel: (userId: string) => Promise<CachedDMChannel>
Section titled “getDMChannel: (userId: string) => Promise<CachedDMChannel>”Fetches a DM channel for a user. Returns the cached value if available, otherwise creates a new DM channel via Discord and caches the result.
setDMChannel: (userId: string, channel: CachedDMChannel, ttlMs?: number) => Promise<void>
Section titled “setDMChannel: (userId: string, channel: CachedDMChannel, ttlMs?: number) => Promise<void>”Manually cache a DM channel for a user. Useful if you have a DM channel object from another source and want to store it in the cache.
Note, that DM channels are stored in two places: the dmChannels namespace keyed by user ID, and the channels namespace keyed by channel ID.
Both must be populated for the cache to function properly. This method handles both automatically.
roles: NamespaceAccessor<APIRole> (readonly)
Section titled “roles: NamespaceAccessor<APIRole> (readonly)”Cached role objects. Roles resolved from interaction data are populated automatically.
users: NamespaceAccessor<APIUser> (readonly)
Section titled “users: NamespaceAccessor<APIUser> (readonly)”Cached user objects. Users found in interaction resolved data and interaction.user / interaction.member.user are populated automatically.
guilds: NamespaceAccessor<APIGuild> (readonly)
Section titled “guilds: NamespaceAccessor<APIGuild> (readonly)”Cached guild objects. Guilds are not populated automatically — the guild object on interactions is partial and only carries id, features, and locale.
members: MemberNamespaceAccessor (readonly)
Section titled “members: MemberNamespaceAccessor (readonly)”Cached guild member objects, scoped by guildId. Members resolved from interaction data are populated automatically.
Public methods
Section titled “Public methods”getGuildRoles(guildId: string): Promise<APIRole[]>
Section titled “getGuildRoles(guildId: string): Promise<APIRole[]>”Returns all cached roles for the given guild. Reads a stored index of role IDs and resolves each one from the role namespace. Returns an empty array if none are cached.
populate(interaction: ValidInteraction): Promise<void>
Section titled “populate(interaction: ValidInteraction): Promise<void>”Extracts and caches users, roles, members, and channels from a raw interaction payload. Called automatically by Honocord before dispatching to handlers when a cache adapter is registered.
NamespaceAccessor<T> interface
Section titled “NamespaceAccessor<T> interface”| Method | Signature | Description |
|---|---|---|
get | (id: string) => Promise<T | null> | Retrieve a cached entity by ID. Returns null if not found or expired. |
set | (data: T, ttlMs?: number) => Promise<void> | Cache an entity. Falls back to defaultTtlMs if ttlMs is omitted. |
delete | (id: string) => Promise<void> | Remove a cached entity by ID. |
has | (id: string) => Promise<boolean> | Check whether a non-expired entry exists for this ID. |
mset | (entries: { value: T; ttlMs?: number }[]) => Promise<void> | Bulk-cache multiple entities. |
MemberNamespaceAccessor interface
Section titled “MemberNamespaceAccessor interface”Same as above but all methods require guildId as the first argument, since members are keyed as member:{guildId}:{userId}.
| Method | Signature |
|---|---|
get | (guildId: string, userId: string) => Promise<CachedGuildMember | null> |
set | (guildId: string, member: CachedGuildMember, ttlMs?: number) => Promise<void> |
delete | (guildId: string, userId: string) => Promise<void> |
has | (guildId: string, userId: string) => Promise<boolean> |
mset | (guildId: string, entries: { value: CachedGuildMember; ttlMs?: number }[]) => Promise<void> |