Skip to content

Commit fe45ee6

Browse files
committed
fix(cache): invalidate public profile cache after platform link mutations
Root cause: createPlatformLink, updatePlatformLink, deletePlatformLink, and reorderLinks all mutated the database but never called redis.del on the profile:<username> cache key, leaving stale data served to viewers until the 5-minute TTL expired naturally. Fix: Add a private invalidateProfileCacheForUser helper that resolves the username via a lightweight SELECT then calls redis.del. All four mutation functions now await this helper after a successful DB write so the cache is cleared immediately. Cache invalidation is skipped when Redis is absent and errors are caught and logged non-fatally so a Redis blip never fails a mutation request. Also fix the DELETE /api/cards/:id route handler which checked error codes as return values; the service throws errors, so the handler now catches them. Fix cards.test.ts duplicate buildApp declaration, and apply the PlatformLink type fix to cardService.ts (upstream/main has not yet merged that PR). Tests: 21 new tests in profile-cache.test.ts cover cache hit/miss lifecycle, all four mutation paths, failed mutations, non-existent links, Redis-absent mode, consecutive mutations, cache repopulation, and non-fatal Redis errors.
1 parent 8e4066e commit fe45ee6

5 files changed

Lines changed: 725 additions & 58 deletions

File tree

apps/backend/src/__tests__/cards.test.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Fastify, { type FastifyInstance, type FastifyRequest } from 'fastify';
1+
import Fastify, { type FastifyInstance } from 'fastify';
22
import { describe, it, expect, beforeEach, vi } from 'vitest';
33

44
import { cardRoutes } from '../routes/cards.js';
@@ -53,10 +53,6 @@ function wireTransaction(): void {
5353
}
5454

5555
async function buildApp(): Promise<FastifyInstance> {
56-
const app = Fastify({ logger: false });
57-
app.decorate('prisma', mockPrisma);
58-
app.decorate('authenticate', async (request: FastifyRequest & { user?: { id: string } }) => {
59-
async function buildApp():Promise<FastifyInstance> {
6056
const app = Fastify({ logger: false });
6157
app.decorate('prisma', mockPrisma as unknown as PrismaClient);
6258
app.decorate('authenticate', async (request: any) => {

0 commit comments

Comments
 (0)