Repository navigation
feat: add @wolfstar/plugin-cache, a storage-agnostic entity cache - #92
Conversation
◈ PR Lens
Architecture 7 components touched across 4 lanes. Play the interactive walkthrough Inside the changed components — 2 viewsComponent view — Gateway Dispatch & Operations Translating gateway dispatches into atomic cache updates and cascading deletions. Component view — Memory & Redis Storage Backends In-memory LRU caching alongside Redis TTL expiration, sorted-set indexing, and compression. Data flow
Follow each request, response and payload The other flows — 1 sequence
View
Tip Untick Architecture lens or Data flow lens under View to hide a diagram, or tick Expand every detail to open every section. The comment redraws in a few seconds 🪧 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. |
|
Review the following changes in direct dependencies. Learn more about Socket for GitHub.
|
There was a problem hiding this comment.
Important
Two correctness bugs in the GUILD_DELETE/entity-lifecycle cascade would ship subtly wrong data: cached guilds carry a permanently stale roles/emojis/stickers snapshot, and a couple of thread-related cache entries leak past a guild's deletion.
Reviewed changes
Reviewed the full diff of a new, self-contained package, @wolfstar/plugin-cache (no existing code calls into it yet — this is the base of a two-PR stack per the PR description).
Cache/EntityCacheinterfaces (src/lib/types.ts) — a Map-like, per-entity,Awaitable-everywhere contract, plus the raw API payload shape stored per entity kind.- Dispatch-to-cache translation (
src/lib/operations.ts) —createCacheOperationsturns a gateway dispatch into a list ofCacheOperations (pure),applyCacheOperations/applyGatewayDispatchapply them, including cascading deletes (e.g.GUILD_DELETEsweeping every guild-scoped entity). - In-memory store (
src/lib/memory.ts) —Map-backed, optional LRUmaxSize. - Redis store (
src/lib/redis.ts) — per-entity TTL via nativePXexpiry plus a sorted-set index for enumeration, optional gzip/brotli compression with a tagged-value scheme so toggling compression doesn't break reading previously-stored values.
⚠️ GUILD_DELETE cascade requires a full scan of guild-independent entity stores
deleteGuildScopedResources's scanned fallback (applicationCommandPermissions, channels, threads, threadMembers, messages, invites — entities keyed by their own ID rather than guildId:...) cleans up via deleteWhere, which calls entries(): a full read of the entire entity store, not just the deleted guild's slice. For RedisEntityCache, that means reading the full sorted-set index and mget-ing every key in that store. For a bot in many guilds with an unbounded or lightly-bounded messages cache, a single GUILD_DELETE (e.g. the bot gets kicked) triggers an O(total cached messages across every guild) scan.
Technical details
# GUILD_DELETE cascade cost scales with total cache size, not guild size
## Affected sites
- `packages/plugin-cache/src/lib/operations.ts` — `deleteGuildScopedResources`'s `scanned` array and the `deleteWhere` case in `applyCacheOperations`
- `packages/plugin-cache/src/lib/redis.ts` — `RedisEntityCache.entries()`, which backs `deleteWhere`
## Required outcome
No hard requirement to change before merge — the PR's stated design deliberately keeps `Cache` "dumb" (no guild index maintained by the store itself), so this is an inherent cost of that tradeoff. Flagging so the author can confirm it's an accepted cost, or note it for the follow-up `GatewayClient` PR (e.g. batching/rate-limiting guild-delete cascades for bots with large multi-guild message caches).
## Open questions for the human
Is the expected scale (guild count × cached-messages-per-guild) small enough that a full-store scan per `GUILD_DELETE` is fine in practice, or should this be called out in the README's caveats alongside the existing `keys`/`values`/`entries` materialization note?ℹ️ Nitpicks
RedisEntityCache.set()(packages/plugin-cache/src/lib/redis.ts) writes the value and updates the sorted-set index in two separate, non-atomic Redis calls; a crash between them leaves the index and the value store slightly out of sync until the next write or expiry. Likely fine givenEntityCache's best-effort semantics, but worth a one-line note if it's a known/accepted tradeoff.
Claude Sonnet | 𝕏
Implements the cache RFC: a Cache interface with one EntityCache per entity, applyGatewayDispatch with cascading deletes, and in-memory (LRU) and Redis (TTL, gzip/brotli compression) stores. Refs #55
64371b6 to
b3e957f
Compare
@wolfstar/plugin-api
@wolfstar/plugin-cache
@wolfstar/plugin-i18next
@wolfstar/plugin-logger
@wolfstar/plugin-subcommands-advanced
commit: |
Thread entries now carry their guild ID too, so GUILD_DELETE sweeps them.
|
Your Claude subscription has hit its usage limit. It resets at 7:20pm (UTC). Re-trigger Pullfrog after the reset, or add an Add repo secret → · Model settings → · Setup docs → · Ask in Discord →
|

🔗 Linked issue
Resolves #55
🧭 Context
stars-components#51 asked for a Gateway package with local and Redis caching plus compression. The cache half got its own RFC (#55) so it can be reviewed without the Gateway client. This is the base of a two-PR stack; the
GatewayClientthat reads from it comes in the next PR.📚 Description
A new package,
@wolfstar/plugin-cache. It stores raw API data only; turning that into Structures is the managers' job in the gateway package. That keeps the same data valid in aMapor in Redis.The dispatch-to-cache table is adapted from suneettipirneni's
@discordjs/cachebranch. The RFC leaned towards keepingCachedumb, so the cascades (a guild delete also drops its channels, members, roles...) live inapplyGatewayDispatchand not in the stores. Any customCachegets them for free.Where I went off the RFC, or picked an answer to one of its open questions:
createRedisCachetakes a client instance, notRedisOptions, so ioredis stays out of the dependencies. A type test checks that ioredisRedisandClusterfitRedisClientLikewithout a cast.EntityCachegotkeys/values/entries, returning array snapshots (Awaitable<T[]>) rather thanAsyncIterable. Simpler to implement for both stores, but a big entity cache gets fully materialised. Open to switching.maxSizeinstead.brotlijoinsgzipas a compression option. Compressed values are tagged, so changing the setting doesn't break reading older values.Key changes
Cache,EntityCacheAwaitableapplyGatewayDispatch/createCacheOperationscreateInMemoryCacheMapstore, optional LRU bound globally or per entitycreateRedisCacheType of Change
Pre-flight Checklist
pnpm changeset(orpnpm changeset add --emptyif no release is needed). See .changeset/README.md.