diff --git a/src/providers/redis.ts b/src/providers/redis.ts index 704eaa4..b58093a 100644 --- a/src/providers/redis.ts +++ b/src/providers/redis.ts @@ -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) } }) @@ -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) } }) diff --git a/tests/redis.test.ts b/tests/redis.test.ts index a4a8c71..be852d1 100644 --- a/tests/redis.test.ts +++ b/tests/redis.test.ts @@ -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') + }) })