From 121f70da97c04b35ea13c1b5b37eacfc50831cb3 Mon Sep 17 00:00:00 2001 From: kooks7 Date: Mon, 26 May 2025 16:10:03 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20multi=EC=82=AC=EC=9A=A9=ED=95=98?= =?UTF-8?q?=EB=8F=84=EB=A1=9D=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/FastCache.ts | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/FastCache.ts b/src/FastCache.ts index f8774d9..f973215 100644 --- a/src/FastCache.ts +++ b/src/FastCache.ts @@ -219,15 +219,20 @@ export class FastCache { return { key, add: async (score: number, value: string): Promise => { - await this.client.zadd(key, score, value); + const multi = this.client.multi(); + multi.zadd(key, score, value); + multi.expire(key, this.ttl); + await multi.exec(); }, addAll: async (entries: Array<{ score: number; value: string }>): Promise => { if (entries.length === 0) { return; } - const args = entries.flatMap((entry) => [entry.score, entry.value]); - await this.client.zadd(key, ...args); + const multi = this.client.multi(); + multi.zadd(key, ...args); + multi.expire(key, this.ttl); + await multi.exec(); }, remove: async (...values: Array): Promise => { await this.client.zrem(key, ...values); @@ -308,12 +313,13 @@ export class FastCache { throw new Error('score is not a number'); } - const tempKey = `${key}:temp:${Date.now()}`; - const args = entries.flatMap((entry) => [entry.score, entry.value]); - await this.client.zadd(tempKey, ...args); - await this.client.expire(tempKey, 60); - await this.client.rename(tempKey, key); + + const multi = this.client.multi(); + multi.del(key); + multi.zadd(key, ...args); + multi.expire(key, this.ttl); + await multi.exec(); }, }; }