Skip to content
Merged
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
22 changes: 14 additions & 8 deletions src/FastCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,15 +219,20 @@ export class FastCache {
return {
key,
add: async (score: number, value: string): Promise<void> => {
await this.client.zadd(key, score, value);
const multi = this.client.multi();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

이 경우 (single add)에도 multi 를 사용하는거에요?

  1. client.zadd 보다 성능이 더 좋거나.
  2. api 디자인 일관성을 위해

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

네 single add에도 expire로 ttl을 설정해주다보니 transaction 처럼 multi를 사용해두었습니다!

multi.zadd(key, score, value);
multi.expire(key, this.ttl);
await multi.exec();
},
addAll: async (entries: Array<{ score: number; value: string }>): Promise<void> => {
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<string>): Promise<void> => {
await this.client.zrem(key, ...values);
Expand Down Expand Up @@ -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();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

데이터베이스 트랜젝션처럼 처리하는군요~ api 디자인이 일관성이 생겨서 좋은 거 같습니다.

multi.del(key);
multi.zadd(key, ...args);
multi.expire(key, this.ttl);
await multi.exec();
},
};
}
Expand Down