Repository navigation
feat(plugin-gateway,plugin-cache): synchronous reads from in-memory caches - #131
Conversation
…y caches EntityCache gains an optional readonly `synchronous` flag, true on MemoryEntityCache and false on RedisEntityCache (compressed or not); a store leaving it out is treated as asynchronous. Every CachedManager gains `cached(...ids)`, the synchronous counterpart of `get`: it returns the same structure, relations included, or `undefined` on a miss or without a cache, and throws a TypeError when the entity cache, or one of the caches its relations are read from, is asynchronous. Structures are now built by `_hydrate`, which returns an Awaitable and stays synchronous when every cache it reads is, so `get`, `hydrate` and `cached` share a single implementation. The existing asynchronous methods keep their behavior. Refs #121 Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01RBWEHLtNzWNykJMPqeqqbB
◈ PR LensNote This drawing shows
Architecture 8 components touched across 5 lanes. Play the interactive walkthrough Inside the changed components — 2 viewsComponent view — Gateway Manager Hydration Synchronous cached entity retrieval and relation hydration across gateway managers. Component view — Cache Store Synchronization Synchronous flag contracts across in-memory and Redis cache backends. Data flow
Follow each request, response and payload View
Tip The CLI's 🪧 More tips
Thanks for using PR Lens! It's built by Coldtea, free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. |
@wolfstar/plugin-api
@wolfstar/plugin-cache
@wolfstar/plugin-gateway
@wolfstar/plugin-i18next
@wolfstar/plugin-logger
@wolfstar/plugin-sharder
@wolfstar/plugin-subcommands-advanced
commit: |
There was a problem hiding this comment.
✅ No new issues found.
Reviewed changes
Reviewed the full diff (29 files) end to end, plus surrounding call sites not touched by the PR.
EntityCache.synchronous?: new optional flag inplugin-cache,trueonMemoryEntityCache,falseonRedisEntityCache(all compression modes); absent on a custom store means asynchronous.CachedManager#cached(...args): new synchronous counterpart ofget, returningundefinedon a miss/no-cache and throwingTypeErrorwhen the entity cache — or any cache a relation is resolved from — turns out to be asynchronous.- Internal
_get/_resolveData/_hydrate:get/resolveData/hydratebecome thinasyncwrappers around these, which returnAwaitable<T>via the newwhenAll/isPromiseLikehelpers (util/cache.ts) —Promise.all-shaped but stays synchronous when nothing passed to it is a promise. - 17 relation-resolving managers converted from
override hydratetooverride _hydrate, calling other managers'_get/_resolveDatainstead of the public async methods, so "is this synchronous" propagates correctly through nested relation lookups (author, member, guild, channel, ...). ChannelManager's thread-cache fallback moved from anoverride getintooverride _get, preserving behavior while staying sync-capable.- Tests added for the happy path (relations included), multi-ID reads, thread fallback, no-cache, a Redis cache (throws), and a mixed sync/async cache (throws) — all with exact assertions, not loose checks.
- READMEs (
plugin-cache,plugin-gateway) and aminorchangeset for both packages document the new surface.
I traced the whenAll/isPromiseLike propagation by hand against nested and mixed-cache cases, and grepped all of packages/plugin-gateway/src/managers for any leftover override hydrate/override async get — none remain, confirming all 17 managers (matching the PR's own count) were converted consistently. The few surviving .get(/.resolveData(/.hydrate( call sites outside the diff are unrelated one-off fetch helpers, not part of any relation-resolution chain, and correctly untouched.
Claude Sonnet | 𝕏

🔗 Linked issue
Part of #121 (item 8: every cache read is async, even in memory).
🧭 Context
EntityCachetypes every method asAwaitable<T>, so the in-memory and Redis stores can share one contract, andMemoryEntityCache#getalready returns synchronously. But everyCachedManagermethod in plugin-gateway isasync. Code ported from discord.js therefore has to await everyguild.members.cache.get(id), even in hot paths like message filters. Callers also had no way to know when a synchronous read was possible, because you can't tell anAwaitableapart from a promise without calling the method.📚 Description
This adds an opt-in synchronous path for synchronous caches. None of the existing async methods change behavior.
EntityCache.synchronous?: a new optional readonly flag.trueonMemoryEntityCache(createInMemoryCache) andfalseonRedisEntityCache(createRedisCache, with or without compression).CachedManager#cached(...ids): the synchronous counterpart ofget. It takes the same arguments and builds the same structure, relations included. For example,client.members.cached(guildId, userId)orclient.messages.cached(channelId, messageId).client.channels.cached(id)falls back to the threads cache, likegetdoes.cachedcan't answer:undefinedon a miss, or when the client has no cache, likeget.TypeError. Such a cache can't say whether the entity is there without waiting. Returningundefinedwould quietly disable whatever the caller guards with it (a message filter, say) after a move to Redis.Cachethat mixes both kinds. The dropped promise gets a.catchso it never becomes an unhandled rejection.manager.cache?.synchronoustells callers which path to take.whenAllhelper. It works likePromise.all(values).then(cb)but stays synchronous when none of the values is a promise.get/hydrate/resolveDatanow return the internal_get/_hydrate/_resolveData, sogetandcachedshare one builder and can't drift apart._hydrateinstead ofhydrate.Worth knowing when reviewing:
getByKeyandcachedGuildnow return a value or a promise instead of always aPromise. Every caller in this package awaits them. An outside subclass calling.then()on them would need toawaitinstead..oxlintrc.jsonadds_get/_hydrate/_resolveDatato plugin-gateway'sno-underscore-dangleallow list, next to_add. Like_add, they are@internalmethods that follow discord.js's underscore convention.Key changes
EntityCache.synchronous?;MemoryEntityCache.synchronous = true;RedisEntityCache.synchronous = false; README "Custom stores"CachedManager#cached; internal_get/_resolveData/_hydrate; relation-resolving managers override_hydrate;ChannelManagerthread fallback moved into_get;whenAll/isPromiseLikeinutil/cache.ts; README "Synchronous reads"cachedequals whatgetresolves, with relations; multi-id reads; thread fallback; no cache; Redis cache (throws); mixed cache (throws)minorfor both packagesType of Change
Pre-flight Checklist
main)pnpm changesetAlso checked:
pnpm lint,pnpm typecheck, and both package builds (attw and publint clean).This PR and #126 (member requests) both touch
GuildMemberManager.ts, in separate places. Whichever merges second may need a merge ofmain.🤖 Generated with Claude Code
https://claude.ai/code/session_01RBWEHLtNzWNykJMPqeqqbB
Generated by Claude Code