Files
outline/server/utils/RedisPrefixHelper.ts
Tom MoorandClaude Opus 5 399cd4041d perf: Reduce database and Redis round-trips on the MCP endpoint (#13333)
* perf: Reduce database and Redis round-trips on the MCP endpoint

Profiling production traces for the /mcp endpoint showed the time is
spent in repeated permission and breadcrumb lookups rather than in the
MCP layer itself.

- Cache Document.membershipDocumentIds in Redis, mirroring the TTL and
  key pattern of User.collectionIds. The two are resolved together for
  every search and this half was uncached, measuring ~200ms.
- Resolve each collection's cached document structure once, concurrently,
  when building breadcrumbs for a batch of documents. It was awaited per
  document inside a loop, so a page of search results meant a serial
  Redis round-trip for every result.
- Walk each collection structure once into a document ID to breadcrumb
  map instead of searching the whole tree per document.
- Reuse the collection association already loaded by Document.findByPk
  when resolving a single document's breadcrumb, rather than querying it
  again under the same membership scope.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* fix: Invalidate cached document membership IDs on permission changes

The cache added alongside membershipDocumentIds relied on its TTL alone,
so a revoked grant stayed searchable until it expired. Clear the key on
the paths that change which documents a user is a member of, keeping the
TTL as a backstop.

- UserMembership create and destroy where documentId is set.
- GroupMembership create and destroy where documentId is set, for every
  user in the group.
- GroupUser create and destroy, as joining or leaving a group changes
  which group grants reach the user.

Sourced memberships are written with hooks disabled and destroyed without
individual hooks, so the parent membership invalidates once for the group
rather than once per descendant.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-07 19:01:02 -04:00

95 lines
2.7 KiB
TypeScript

/**
* Helper class for Redis cache key generation.
*/
export class RedisPrefixHelper {
/**
* Gets key against which unfurl response for the given url is stored.
*
* @param teamId The team ID to generate a key for.
* @param url The url to generate a key for.
*/
public static getUnfurlKey(teamId: string, url = "") {
return `unfurl:${teamId}:${url}`;
}
/**
* Gets key for caching collection documents structure.
*
* @param collectionId The collection ID to generate a key for.
* @returns the cache key string.
*/
public static getCollectionDocumentsKey(collectionId: string) {
return `cd:${collectionId}`;
}
/**
* Gets key for caching embed check results. This is a global cache key
* (not team-specific) since embed headers are the same for all users.
*
* @param url The URL to generate a cache key for.
* @returns the cache key string.
*/
public static getEmbedCheckKey(url: string) {
return `embed:${url}`;
}
/**
* Gets key for caching a user's accessible collection IDs.
*
* @param userId The user ID to generate a key for.
* @returns the cache key string.
*/
public static getUserCollectionIdsKey(userId: string) {
return `uc:${userId}`;
}
/**
* Gets key for caching the document IDs a user is a member of.
*
* @param userId The user ID to generate a key for.
* @returns the cache key string.
*/
public static getUserMembershipDocumentIdsKey(userId: string) {
return `ud:${userId}`;
}
/**
* Gets key for caching a team's enabled webhook subscriptions.
*
* @param teamId The team ID to generate a key for.
* @returns the cache key string.
*/
public static getWebhookSubscriptionsKey(teamId: string) {
return `whs:${teamId}`;
}
/**
* Gets key for caching the count of a relationship managed by the
* `CounterCache` decorator.
*
* @param modelName The owning model name (e.g. "Group").
* @param relationName The relationship reference name (e.g. "members").
* @param id The owning record id.
* @returns the cache key string.
*/
public static getCounterCacheKey(
modelName: string,
relationName: string,
id: string
) {
return `count:${modelName}:${relationName}:${id}`;
}
/**
* Gets key for storing an auth provider's token used as a logout hint during
* provider-initiated logout, referenced by a short session identifier.
*
* @param provider The auth provider id (e.g. "oidc").
* @param sessionId The logout session identifier to generate a key for.
* @returns the cache key string.
*/
public static getLogoutTokenKey(provider: string, sessionId: string) {
return `auth:logout:${provider}:${sessionId}`;
}
}