Skip to content

feat: add @wolfstar/plugin-cache, a storage-agnostic entity cache - #92

Merged
RedStar071 merged 2 commits into
mainfrom
feat/plugin-cache
Sep 25, 2026
Merged

RedStar071 merged 2 commits into
mainfrom
feat/plugin-cache

Conversation

@RedStar071

Copy link
Copy Markdown
Member

🔗 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 GatewayClient that 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 a Map or in Redis.

The dispatch-to-cache table is adapted from suneettipirneni's @discordjs/cache branch. The RFC leaned towards keeping Cache dumb, so the cascades (a guild delete also drops its channels, members, roles...) live in applyGatewayDispatch and not in the stores. Any custom Cache gets them for free.

Where I went off the RFC, or picked an answer to one of its open questions:

  • createRedisCache takes a client instance, not RedisOptions, so ioredis stays out of the dependencies. A type test checks that ioredis Redis and Cluster fit RedisClientLike without a cast.
  • EntityCache got keys/values/entries, returning array snapshots (Awaitable<T[]>) rather than AsyncIterable. Simpler to implement for both stores, but a big entity cache gets fully materialised. Open to switching.
  • TTL is a Redis-only option. The in-memory store gets an LRU maxSize instead.
  • brotli joins gzip as a compression option. Compressed values are tagged, so changing the setting doesn't break reading older values.

Key changes

Export What it does
Cache, EntityCache Per-entity, Map-like interface, every method Awaitable
applyGatewayDispatch / createCacheOperations Dispatch to cache operations, cascades included
createInMemoryCache Map store, optional LRU bound globally or per entity
createRedisCache Redis store, per-entity TTL, gzip or brotli above a size threshold

Type of Change

  • New feature (non-breaking change which adds functionality)
  • Bug fix (non-breaking change which fixes an issue)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation change
  • Workflow / CI change

Pre-flight Checklist

  • I have read the Contributing Guidelines (if applicable)
  • My code follows the code style of this project
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • I have updated the documentation accordingly
  • I added a changeset with pnpm changeset (or pnpm changeset add --empty if no release is needed). See .changeset/README.md.

🤖 AI disclosure: Wolfstar Agent Kit modified this description. My AI open-source policy.

@coldtea-pr-lens

coldtea-pr-lens Bot commented Sep 23, 2026 •

Copy link
Copy Markdown

◈ PR Lens

🟢 +6 new · 🟠 ~1 changed · 🔴 -0 removed · 2 flows · 14 files · commit 135930b


Architecture

Architecture diagram for wolfstar-project/plugins at 135930b

7 components touched across 4 lanes.

Play the interactive walkthrough


Inside the changed components — 2 views

Component view — Gateway Dispatch & Operations

Translating gateway dispatches into atomic cache updates and cascading deletions.

Architecture view of Component view — Gateway Dispatch & Operations in wolfstar-project/plugins

Component view — Memory & Redis Storage Backends

In-memory LRU caching alongside Redis TTL expiration, sorted-set indexing, and compression.

Architecture view of Component view — Memory & Redis Storage Backends in wolfstar-project/plugins

Data flow

Data flow diagram for wolfstar-project/plugins at 135930b

Hydrating Gateway Dispatches into Cache · Writing Compressed Entries to Redis

Follow each request, response and payload


The other flows — 1 sequence

Writing Compressed Entries to Redis

Sequence diagram of Writing Compressed Entries to Redis in wolfstar-project/plugins

View

  • Architecture lens
  • Data flow lens
  • Expand every detail

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
  • Run npx skills add coldteadotai/pr-lens, then tell your coding agent: "Diagram the change you just made with PR Lens and attach it to the pull request."
  • Run npx @coldtea/pr-lens-cli analyze --base origin/main on a branch, then npx @coldtea/pr-lens-cli render .pr-lens/graph.json. Same lenses, your own model key, before the pull request exists
  • Click the link under each diagram to open it on a canvas you can zoom, pan and step through
  • The diagrams are links. Click one to open it on the canvas, then press W or click play to walk through the change
  • Open a diagram on the canvas, then press W or click play to walk through the change one step at a time
  • The CLI's render reads .github/pr-lens.yml and applies your renames, exclusions and lane pins at draw time
  • Set github.comment.collapsed: true in .github/pr-lens.yml to fold the comment behind one View architecture and data flow row. Drawing still runs on every push
  • Set github.draw: on-demand in .github/pr-lens.yml and PR Lens stops drawing every push. Comment @pr-lens draw on a pull request when you want that one drawn
  • Add .github/workflows/pr-lens.yml with coldteadotai/pr-lens/packages/action@v0 and your model provider's key as its api-key to run PR Lens from your own CI. Any /chat/completions endpoint works
  • Push a commit and the comment redraws for the new head. A slow older run never overwrites a newer one
  • Switch GitHub to dark mode and the diagrams follow. The moving dots are this pull request's data in motion

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.

❤️ Share

@socket-security

socket-security Bot commented Sep 23, 2026 •

Copy link
Copy Markdown

Review the following changes in direct dependencies. Learn more about Socket for GitHub.

Diff Package Supply Chain
Security
Vulnerability Quality Maintenance License
Addedioredis@​6.0.09410010091100

View full report

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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/EntityCache interfaces (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) — createCacheOperations turns a gateway dispatch into a list of CacheOperations (pure), applyCacheOperations/applyGatewayDispatch apply them, including cascading deletes (e.g. GUILD_DELETE sweeping every guild-scoped entity).
  • In-memory store (src/lib/memory.ts) — Map-backed, optional LRU maxSize.
  • Redis store (src/lib/redis.ts) — per-entity TTL via native PX expiry 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 given EntityCache's best-effort semantics, but worth a one-line note if it's a known/accepted tradeoff.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using Claude Sonnet | 𝕏

@RedStar071 RedStar071 changed the title feat(plugin-cache): add storage-agnostic Discord entity cache feat: add @wolfstar/plugin-cache, a storage-agnostic entity cache Sep 23, 2026
@RedStar071
RedStar071 added this pull request to stack #96 September 24, 2026 14:56
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
@pkg-pr-new

pkg-pr-new Bot commented Sep 25, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

@wolfstar/plugin-api

pnpm add https://pkg.pr.new/wolfstar-project/plugins/@wolfstar/plugin-api@92

@wolfstar/plugin-cache

pnpm add https://pkg.pr.new/wolfstar-project/plugins/@wolfstar/plugin-cache@92

@wolfstar/plugin-i18next

pnpm add https://pkg.pr.new/wolfstar-project/plugins/@wolfstar/plugin-i18next@92

@wolfstar/plugin-logger

pnpm add https://pkg.pr.new/wolfstar-project/plugins/@wolfstar/plugin-logger@92

@wolfstar/plugin-subcommands-advanced

pnpm add https://pkg.pr.new/wolfstar-project/plugins/@wolfstar/plugin-subcommands-advanced@92

commit: 135930b

Thread entries now carry their guild ID too, so GUILD_DELETE sweeps them.
@pullfrog

pullfrog Bot commented Sep 25, 2026

Copy link
Copy Markdown
Contributor

Your Claude subscription has hit its usage limit. It resets at 7:20pm (UTC). Re-trigger Pullfrog after the reset, or add an ANTHROPIC_API_KEY repo secret — Pullfrog routes around an exhausted subscription automatically when one is present.

Add repo secret → · Model settings → · Setup docs → · Ask in Discord →

Pullfrog  | Rerun failed job ➔ | View workflow run | via Pullfrog | Using Claude Sonnet | 𝕏

@RedStar071
RedStar071 merged commit 6da7e99 into main Sep 25, 2026
16 of 17 checks passed
@RedStar071
RedStar071 deleted the feat/plugin-cache branch September 25, 2026 17:51
@RedStar071
RedStar071 restored the feat/plugin-cache branch September 25, 2026 17:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RFC: Pluggable cache layer for @wolfstar/plugin-gateway (Cache, EntityCache, CachedManager)

1 participant