fix: Scope external unfurl cache per user (#14267)

* fix: Scope external unfurl cache per user

* test: Remove teammate unfurl cache test
This commit is contained in:
Tom Moor authored and GitHub committed 2026-10-23 21:47:48 -04:00
1 parent 41ba5c9fcb
commit 9493f89f1e
7 files changed
+56 -23

No files matched your search

+1 -1
View File
@@ -38,7 +38,7 @@ export async function start(id: number, disconnect: () => void) {
// Clear unfurl cache in development so code changes take effect immediately
if (env.isDevelopment) {
void CacheHelper.clearData(RedisPrefixHelper.getUnfurlKey(""));
void CacheHelper.clearData(RedisPrefixHelper.getUnfurlPrefix());
}
// Find if SSL certs are available
@@ -27,7 +27,7 @@ export default class IntegrationCreatedProcessor extends BaseProcessor {
// Clear the cache of unfurled data for the team as it may be stale now.
await CacheHelper.clearData(
RedisPrefixHelper.getUnfurlKey(integration.teamId)
RedisPrefixHelper.getUnfurlPrefix(integration.teamId)
);
}
}
@@ -28,7 +28,7 @@ export default class IntegrationDeletedProcessor extends BaseProcessor {
// Clear the cache of unfurled data for the team as it may be stale now.
if (integration.type === IntegrationType.Embed) {
await CacheHelper.clearData(
RedisPrefixHelper.getUnfurlKey(integration.teamId)
RedisPrefixHelper.getUnfurlPrefix(integration.teamId)
);
}
+2 -2
View File
@@ -590,7 +590,7 @@ Install instructions here.`
expect(res.status).toEqual(204);
const ttl = await Redis.defaultClient.ttl(
RedisPrefixHelper.getUnfurlKey(user.teamId, url)
RedisPrefixHelper.getUnfurlKey(user.teamId, user.id, url)
);
expect(ttl).toBeGreaterThan(Hour.seconds);
});
@@ -608,7 +608,7 @@ Install instructions here.`
expect(res.status).toEqual(204);
const ttl = await Redis.defaultClient.ttl(
RedisPrefixHelper.getUnfurlKey(user.teamId, url)
RedisPrefixHelper.getUnfurlKey(user.teamId, user.id, url)
);
expect(ttl).toBeGreaterThan(0);
expect(ttl).toBeLessThanOrEqual(60);
+5 -1
View File
@@ -193,7 +193,11 @@ router.post(
// External resources
// Use getDataOrSet which handles distributed locking to prevent thundering herd
// when multiple clients request the same URL simultaneously
const cacheKey = RedisPrefixHelper.getUnfurlKey(actor.teamId, url);
const cacheKey = RedisPrefixHelper.getUnfurlKey(
actor.teamId,
actor.id,
url
);
const defaultCacheExpiry = 3600;
const unfurlResult = await CacheHelper.getDataOrSet<
+29 -14
View File
@@ -2,27 +2,42 @@ import { RedisPrefixHelper } from "./RedisPrefixHelper";
describe("RedisPrefixHelper", () => {
describe("getUnfurlKey", () => {
it("should generate key with teamId and url", () => {
const teamId = "team-123";
const url = "https://example.com";
const result = RedisPrefixHelper.getUnfurlKey(teamId, url);
expect(result).toBe("unfurl:team-123:https://example.com");
});
it("should generate key with teamId and empty url", () => {
const teamId = "team-456";
const result = RedisPrefixHelper.getUnfurlKey(teamId);
expect(result).toBe("unfurl:team-456:");
it("should generate key with teamId, userId and url", () => {
const result = RedisPrefixHelper.getUnfurlKey(
"team-123",
"user-123",
"https://example.com"
);
expect(result).toBe("unfurl:team-123:user-123:https://example.com");
});
it("should handle special characters in url", () => {
const teamId = "team-789";
const url = "https://example.com/path?query=value&other=123";
const result = RedisPrefixHelper.getUnfurlKey(teamId, url);
const result = RedisPrefixHelper.getUnfurlKey("team-789", "user-1", url);
expect(result).toBe(
"unfurl:team-789:https://example.com/path?query=value&other=123"
"unfurl:team-789:user-1:https://example.com/path?query=value&other=123"
);
});
it("should start with the team prefix", () => {
const result = RedisPrefixHelper.getUnfurlKey("team-1", "user-1", "url");
expect(
result.startsWith(RedisPrefixHelper.getUnfurlPrefix("team-1"))
).toBe(true);
expect(result.startsWith(RedisPrefixHelper.getUnfurlPrefix())).toBe(true);
});
});
describe("getUnfurlPrefix", () => {
it("should generate prefix with teamId", () => {
expect(RedisPrefixHelper.getUnfurlPrefix("team-456")).toBe(
"unfurl:team-456:"
);
});
it("should generate prefix for all teams", () => {
expect(RedisPrefixHelper.getUnfurlPrefix()).toBe("unfurl:");
});
});
describe("getCollectionDocumentsKey", () => {
+17 -3
View File
@@ -3,13 +3,27 @@
*/
export class RedisPrefixHelper {
/**
* Gets key against which unfurl response for the given url is stored.
* Gets key against which unfurl response for the given url is stored. The
* key includes the user as unfurls can depend on the user's linked accounts.
*
* @param teamId The team ID to generate a key for.
* @param userId The user ID to generate a key for.
* @param url The url to generate a key for.
* @returns the cache key string.
*/
public static getUnfurlKey(teamId: string, url = "") {
return `unfurl:${teamId}:${url}`;
public static getUnfurlKey(teamId: string, userId: string, url: string) {
return `${RedisPrefixHelper.getUnfurlPrefix(teamId)}${userId}:${url}`;
}
/**
* Gets the prefix shared by all unfurl keys of a team, or of all teams if no
* team is given.
*
* @param teamId The team ID to generate a prefix for.
* @returns the cache key prefix string.
*/
public static getUnfurlPrefix(teamId?: string) {
return teamId ? `unfurl:${teamId}:` : "unfurl:";
}
/**