Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/providers/redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export class RedisCacheProvider implements CacheProvider {
})

stream.on('data', async (keys: string[]) => {
if (keys.length > 1) {
if (keys.length > 0) {
await this.redis.del(...keys)
}
})
Expand All @@ -81,7 +81,7 @@ export class RedisCacheProvider implements CacheProvider {
})

stream.on('data', async (keys: string[]) => {
if (keys.length > 1) {
if (keys.length > 0) {
await this.redis.del(...keys)
}
})
Expand Down
36 changes: 36 additions & 0 deletions tests/redis.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1191,4 +1191,40 @@ describe('Cache plugin (redis)', () => {
}),
).resolves.toBe(true)
})

it('invalidates correctly when there is only 1 entry', async () => {
let extDb = db.$use(
defineCachePlugin({
provider: new RedisCacheProvider({
url: process.env['REDIS_URL'] as string,
}),
}),
)

await extDb.user.create({
data: {
email: 'test@email.com',
},
})

await extDb.user.exists({
cache: {
tags: ['user1'],
ttl: 60,
},
})

await extDb.$cache.invalidate({
tags: ['user1'],
})

await extDb.user.exists({
cache: {
tags: ['user1'],
ttl: 60,
},
})

expect(extDb.$cache.status).toBe('miss')
Comment on lines +1210 to +1228

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Synchronize the seed write before invalidating.

The first cached read schedules cache.set asynchronously in src/plugin.ts without awaiting it. Calling invalidate immediately can scan before the single entry or its tag membership exists, allowing the later read to hit for reasons unrelated to this fix. Add a deterministic synchronization step or await the cache write through the test setup.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/redis.test.ts` around lines 1210 - 1228, Update the cache invalidation
test around extDb.user.exists and extDb.$cache.invalidate to deterministically
wait for the first read’s asynchronous cache.set/tag registration to complete
before invalidating. Use the existing test setup or cache synchronization
mechanism rather than relying on timing, then preserve the final miss assertion.

})
})