-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBaseMap.ts
More file actions
201 lines (163 loc) · 5.92 KB
/
Copy pathBaseMap.ts
File metadata and controls
201 lines (163 loc) · 5.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import { factory } from "di-factory";
import { getRedis } from "../../config/redis";
import { inject } from "../core/di";
import LoggerService from "../services/base/LoggerService";
import TYPES from "../core/types";
const ITERATOR_BATCH_SIZE = 100;
const DEFAULT_TTL_EXPIRE_SECONDS = 5 * 60;
export const BaseMap = factory(
class BaseMap {
readonly loggerService = inject<LoggerService>(TYPES.loggerService);
constructor(
readonly connectionKey: string,
readonly ttlExpireSeconds: number = DEFAULT_TTL_EXPIRE_SECONDS
) {}
_getItemKey(key: string): string {
return `${this.connectionKey}:${key}`;
}
async set(key: string, value: unknown): Promise<void> {
if (!key) throw new Error("Key cannot be empty");
this.loggerService.info(`BaseMap set key=${key}`, { key, value });
const redis = await getRedis();
const itemKey = this._getItemKey(key);
await redis.set(itemKey, value as string);
if (this.ttlExpireSeconds !== -1) {
await redis.expire(itemKey, this.ttlExpireSeconds);
}
}
async get(key: string | null): Promise<unknown | null> {
this.loggerService.info(`BaseMap get key=${key}`);
if (key === null) {
return null;
}
const redis = await getRedis();
const value = await redis.get(this._getItemKey(key));
return value ?? null;
}
async delete(key: string): Promise<void> {
this.loggerService.info(`BaseMap delete key=${key}`);
if (key === null) {
return null;
}
const redis = await getRedis();
await redis.del(this._getItemKey(key));
}
async has(key: string): Promise<boolean> {
this.loggerService.info(`BaseMap has key=${key}`);
if (key === null) {
return false;
}
const redis = await getRedis();
const exists = await redis.exists(this._getItemKey(key));
return exists === 1;
}
async clear(): Promise<void> {
this.loggerService.info(`BaseMap clear`);
const redis = await getRedis();
let cursor: string | number = 0;
const pattern = `${this.connectionKey}:*`;
while (true) {
const [nextCursor, keys] = await redis.scan(cursor, "MATCH", pattern, "COUNT", ITERATOR_BATCH_SIZE);
cursor = nextCursor;
if (keys?.length) {
await redis.del(...keys);
}
if (cursor === "0" || cursor === 0) break;
}
}
async toArray(): Promise<[string, unknown][]> {
this.loggerService.info(`BaseMap toArray`);
const redis = await getRedis();
const result: [string, string][] = [];
let cursor: string | number = 0;
const pattern = `${this.connectionKey}:*`;
while (true) {
const [nextCursor, keys] = await redis.scan(cursor, "MATCH", pattern, "COUNT", ITERATOR_BATCH_SIZE);
cursor = nextCursor;
if (keys?.length) {
const values = await redis.mget(...keys);
for (let i = 0; i < keys.length; i++) {
if (typeof values[i] === "string") {
const key = keys[i].substring(this.connectionKey.length + 1);
result.push([key, values[i]!]);
}
}
}
if (cursor === "0" || cursor === 0) break;
}
return result;
}
async *iterate(): AsyncIterableIterator<readonly [string, unknown]> {
this.loggerService.info(`BaseMap iterate`);
const redis = await getRedis();
let cursor: string | number = 0;
const pattern = `${this.connectionKey}:*`;
while (true) {
const [nextCursor, keys] = await redis.scan(cursor, "MATCH", pattern, "COUNT", ITERATOR_BATCH_SIZE);
cursor = nextCursor;
if (keys?.length) {
const values = await redis.mget(...keys);
for (let i = 0; i < keys.length; i++) {
if (typeof values[i] === "string") {
const key = keys[i].substring(this.connectionKey.length + 1);
yield [key, values[i]!];
}
}
}
if (cursor === "0" || cursor === 0) break;
}
}
async *keys(): AsyncIterableIterator<string> {
this.loggerService.info(`BaseMap iterate keys`);
const redis = await getRedis();
let cursor: string | number = 0;
const pattern = `${this.connectionKey}:*`;
while (true) {
const [nextCursor, keys] = await redis.scan(cursor, "MATCH", pattern, "COUNT", ITERATOR_BATCH_SIZE);
cursor = nextCursor;
if (keys?.length) {
for (const fullKey of keys) {
const key = fullKey.substring(this.connectionKey.length + 1);
yield key;
}
}
if (cursor === "0" || cursor === 0) break;
}
}
async *values(): AsyncIterableIterator<unknown> {
this.loggerService.info(`BaseMap iterate values`);
const redis = await getRedis();
let cursor: string | number = 0;
const pattern = `${this.connectionKey}:*`;
while (true) {
const [nextCursor, keys] = await redis.scan(cursor, "MATCH", pattern, "COUNT", ITERATOR_BATCH_SIZE);
cursor = nextCursor;
if (keys?.length) {
const values = await redis.mget(...keys);
for (const value of values) {
if (typeof value === "string") {
yield value;
}
}
}
if (cursor === "0" || cursor === 0) break;
}
}
async size(): Promise<number> {
this.loggerService.info(`BaseMap size`);
const redis = await getRedis();
let cursor: string | number = 0;
const pattern = `${this.connectionKey}:*`;
let count = 0;
while (true) {
const [nextCursor, keys] = await redis.scan(cursor, "MATCH", pattern, "COUNT", ITERATOR_BATCH_SIZE);
cursor = nextCursor;
count += keys.length;
if (cursor === "0" || cursor === 0) break;
}
return count;
}
}
);
export type TBaseMap = InstanceType<ReturnType<typeof BaseMap>>;
export default BaseMap;