-
Notifications
You must be signed in to change notification settings - Fork 210
fix(rpc): align funnel cache invalidation key and re-enable caching #636
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
FindMalek
wants to merge
3
commits into
databuddy-analytics:staging
Choose a base branch
from
FindMalek:fix/funnels-cache-invalidation
base: staging
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| import { beforeEach, describe, expect, it, mock } from "bun:test"; | ||
| import { createDrizzleCache } from "@databuddy/redis/drizzle-cache"; | ||
|
|
||
| const kv = new Map<string, string>(); | ||
| const sets = new Map<string, Set<string>>(); | ||
|
|
||
| const redis = { | ||
| get: mock(async (key: string) => kv.get(key) ?? null), | ||
| setex: mock(async (key: string, _ttl: number, value: string) => { | ||
| kv.set(key, value); | ||
| return "OK" as const; | ||
| }), | ||
| sadd: mock(async (key: string, ...members: string[]) => { | ||
| const set = sets.get(key) ?? new Set<string>(); | ||
| for (const member of members) { | ||
| set.add(member); | ||
| } | ||
| sets.set(key, set); | ||
| return members.length; | ||
| }), | ||
| smembers: mock(async (key: string) => Array.from(sets.get(key) ?? [])), | ||
| srem: mock(async (key: string, member: string) => { | ||
| sets.get(key)?.delete(member); | ||
| return 1; | ||
| }), | ||
| unlink: mock(async (...keys: string[]) => { | ||
| for (const key of keys) { | ||
| kv.delete(key); | ||
| sets.delete(key); | ||
| } | ||
| return keys.length; | ||
| }), | ||
| sunion: mock(async (...keys: string[]) => { | ||
| const out = new Set<string>(); | ||
| for (const key of keys) { | ||
| for (const member of sets.get(key) ?? []) { | ||
| out.add(member); | ||
| } | ||
| } | ||
| return Array.from(out); | ||
| }), | ||
| }; | ||
|
|
||
| mock.module("@databuddy/redis", () => ({ | ||
| createDrizzleCache, | ||
| invalidateAgentContextSnapshotsForWebsite: mock(async () => undefined), | ||
| redis, | ||
| })); | ||
|
|
||
| const { funnelCache, invalidateFunnelsCache } = await import("./funnels-cache"); | ||
|
|
||
| beforeEach(() => { | ||
| kv.clear(); | ||
| sets.clear(); | ||
| }); | ||
|
|
||
| describe("invalidateFunnelsCache", () => { | ||
| it("invalidates the exact key a cached getById lookup is stored under", async () => { | ||
| const funnelId = "funnel-1"; | ||
| let calls = 0; | ||
|
|
||
| const readFunnel = () => | ||
| funnelCache.withCache({ | ||
| key: `byId:${funnelId}`, | ||
| queryFn: async () => { | ||
| calls += 1; | ||
| return { id: funnelId, name: `v${calls}` }; | ||
| }, | ||
| tables: ["funnelDefinitions"], | ||
| ttl: 300, | ||
| }); | ||
|
|
||
| expect(await readFunnel()).toEqual({ id: funnelId, name: "v1" }); | ||
| // Cache hit: queryFn must not run again. | ||
| expect(await readFunnel()).toEqual({ id: funnelId, name: "v1" }); | ||
| expect(calls).toBe(1); | ||
|
|
||
| await invalidateFunnelsCache("website-1", funnelId); | ||
|
|
||
| // Regression guard: invalidateFunnelsCache must target the same key | ||
| // format the router actually caches under (`byId:<funnelId>`), not a | ||
| // key that was never written (e.g. `byId:<funnelId>:<websiteId>`). | ||
| expect(await readFunnel()).toEqual({ id: funnelId, name: "v2" }); | ||
| expect(calls).toBe(2); | ||
| }); | ||
|
|
||
| it("invalidates the exact key a cached list lookup is stored under", async () => { | ||
| const websiteId = "website-2"; | ||
| let calls = 0; | ||
|
|
||
| const readList = () => | ||
| funnelCache.withCache({ | ||
| key: `list:${websiteId}`, | ||
| queryFn: async () => { | ||
| calls += 1; | ||
| return [{ id: `funnel-${calls}` }]; | ||
| }, | ||
| tables: ["funnelDefinitions"], | ||
| ttl: 300, | ||
| }); | ||
|
|
||
| await readList(); | ||
| await readList(); | ||
| expect(calls).toBe(1); | ||
|
|
||
| await invalidateFunnelsCache(websiteId); | ||
|
|
||
| await readList(); | ||
| expect(calls).toBe(2); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The RPC package's configured test command does not select
src/lib/funnels-cache.test.tsor the completesrc/libdirectory, so this regression coverage is skipped by the standard package and CI test task.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!