diff --git a/server/main.ts b/server/main.ts index 80d474dfff..6705ce865a 100644 --- a/server/main.ts +++ b/server/main.ts @@ -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 diff --git a/server/queues/processors/IntegrationCreatedProcessor.ts b/server/queues/processors/IntegrationCreatedProcessor.ts index 42c9720cbd..cc83ff5943 100644 --- a/server/queues/processors/IntegrationCreatedProcessor.ts +++ b/server/queues/processors/IntegrationCreatedProcessor.ts @@ -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) ); } } diff --git a/server/queues/processors/IntegrationDeletedProcessor.ts b/server/queues/processors/IntegrationDeletedProcessor.ts index 33067233fe..ffebdadeb1 100644 --- a/server/queues/processors/IntegrationDeletedProcessor.ts +++ b/server/queues/processors/IntegrationDeletedProcessor.ts @@ -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) ); } diff --git a/server/routes/api/urls/urls.test.ts b/server/routes/api/urls/urls.test.ts index 2436938984..e2c58aac49 100644 --- a/server/routes/api/urls/urls.test.ts +++ b/server/routes/api/urls/urls.test.ts @@ -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); diff --git a/server/routes/api/urls/urls.ts b/server/routes/api/urls/urls.ts index 20eedc0b32..51d30c0a43 100644 --- a/server/routes/api/urls/urls.ts +++ b/server/routes/api/urls/urls.ts @@ -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< diff --git a/server/utils/RedisPrefixHelper.test.ts b/server/utils/RedisPrefixHelper.test.ts index 4b3288dbd1..d1ded1e4be 100644 --- a/server/utils/RedisPrefixHelper.test.ts +++ b/server/utils/RedisPrefixHelper.test.ts @@ -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", () => { diff --git a/server/utils/RedisPrefixHelper.ts b/server/utils/RedisPrefixHelper.ts index 5e8b529ac0..695d194ea2 100644 --- a/server/utils/RedisPrefixHelper.ts +++ b/server/utils/RedisPrefixHelper.ts @@ -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:"; } /**