Skip to content

Commit b30dbc1

Browse files
committed
feat: multi사용하도록 변경
1 parent b609efe commit b30dbc1

1 file changed

Lines changed: 14 additions & 8 deletions

File tree

src/FastCache.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -219,15 +219,20 @@ export class FastCache {
219219
return {
220220
key,
221221
add: async (score: number, value: string): Promise<void> => {
222-
await this.client.zadd(key, score, value);
222+
const multi = this.client.multi();
223+
multi.zadd(key, score, value);
224+
multi.expire(key, this.ttl);
225+
await multi.exec();
223226
},
224227
addAll: async (entries: Array<{ score: number; value: string }>): Promise<void> => {
225228
if (entries.length === 0) {
226229
return;
227230
}
228-
229231
const args = entries.flatMap((entry) => [entry.score, entry.value]);
230-
await this.client.zadd(key, ...args);
232+
const multi = this.client.multi();
233+
multi.zadd(key, ...args);
234+
multi.expire(key, this.ttl);
235+
await multi.exec();
231236
},
232237
remove: async (...values: Array<string>): Promise<void> => {
233238
await this.client.zrem(key, ...values);
@@ -308,12 +313,13 @@ export class FastCache {
308313
throw new Error('score is not a number');
309314
}
310315

311-
const tempKey = `${key}:temp:${Date.now()}`;
312-
313316
const args = entries.flatMap((entry) => [entry.score, entry.value]);
314-
await this.client.zadd(tempKey, ...args);
315-
await this.client.expire(tempKey, 60);
316-
await this.client.rename(tempKey, key);
317+
318+
const multi = this.client.multi();
319+
multi.del(key);
320+
multi.zadd(key, ...args);
321+
multi.expire(key, this.ttl);
322+
await multi.exec();
317323
},
318324
};
319325
}

0 commit comments

Comments
 (0)