diff --git a/README.md b/README.md index cad5a9c..847242a 100644 --- a/README.md +++ b/README.md @@ -142,7 +142,7 @@ const revalidatedPublishedPosts = await client.$cache.revalidation as Post[] You can cache results forever by specifying neither `ttl` nor `swr`. Such results will always be considered fresh. ```typescript -client.post.findMany({ +await client.post.findMany({ cache: { tags: [`user:${userId}`], }, diff --git a/package.json b/package.json index 5733811..e1fae53 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@visualbravo/zenstack-cache", - "version": "1.0.7", + "version": "1.0.8", "description": "Reduce response times and database load with query-level caching integrated with the ZenStack ORM.", "keywords": [ "accelerate", 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') + }) })