-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsortedSetCache.js
More file actions
267 lines (225 loc) · 8.38 KB
/
sortedSetCache.js
File metadata and controls
267 lines (225 loc) · 8.38 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
const Connection = require("./connection"),
dateMatch = /^(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})T(?<hour>\d{2}):(?<minute>\d{2}):(?<second>\d{2}(?:\.\d*))(?<timezone>Z|(?:\+|-)(?:[\d|:]*))?$/;
// MARK: SortedSetCache
/**
* A class that handles caching for sorted sets.
*/
class SortedSetCache {
// MARK: static async add
/**
* Adds an object to the cache.
* @param {string} key The key to add.
* @param {{score: number, value: any}[]} objs The objects to save.
* @param {Date} [expiration] The date and time to expire the cache.
* @param {string[]} [invalidationLists] A list of invalidation lists to add the key to.
* @returns {Promise} A promise that resolves when the object has been added to the cache.
*/
static async add(key, objs, expiration, invalidationLists) {
let client;
try {
client = await Connection.pool.acquire();
const values = [];
for (const obj of objs) {
values.push(obj.score);
values.push(JSON.stringify(obj.value));
}
await client.zadd(key, ...values);
if (invalidationLists) {
await invalidationLists.reduce(
(prev, list) => prev.then(() => client.sadd(list, key)),
Promise.resolve()
);
}
if (expiration) {
await client.pexpireat(key, expiration.getTime());
}
} finally {
if (client) {
await Connection.pool.release(client);
}
}
}
// MARK: static async combine
/**
* Combines two sorted sets into one.
* @param {string} key The key to combine into.
* @param {string[]} keys The keys to combine.
* @param {Date} [expiration] The date and time to expire the cache.
* @param {string[]} [invalidationLists] A list of invalidation lists to add the key to.
* @returns {Promise} A promise that resolves when the object has been added to the cache.
*/
static async combine(key, keys, expiration, invalidationLists) {
let client;
try {
client = await Connection.pool.acquire();
await client.zunionstore(key, keys.length, ...keys);
if (invalidationLists) {
await invalidationLists.reduce(
(prev, list) => prev.then(() => client.sadd(list, key)),
Promise.resolve()
);
}
if (expiration) {
await client.pexpireat(key, expiration.getTime());
}
} finally {
if (client) {
await Connection.pool.release(client);
}
}
}
// MARK: static async count
/**
* Counts the number of items in the sorted set.
* @param {string} key The key.
* @param {string|number} min The minimum value to count.
* @param {string|number} max The maximum value to count.
* @returns {Promise<number>} A promise that returns the number of items in the sorted set.
*/
static async count(key, min, max) {
let client;
try {
client = await Connection.pool.acquire();
return await client.zcount(key, min, max);
} finally {
if (client) {
await Connection.pool.release(client);
}
}
}
// MARK: static async get
/**
* Retrieves data from a set.
* @param {string} key The key to get the data for.
* @param {number} min The minimum index of the set.
* @param {number} max The maximum index of the set.
* @param {boolean} [withScores] Whether to include scores with the results.
* @returns {Promise<any[]|{value: any, score: number}[]>} A promise that returns the objects.
*/
static async get(key, min, max, withScores) {
let client;
try {
client = await Connection.pool.acquire();
if (withScores) {
const items = await client.zrange(key, min, max, "WITHSCORES");
if (!items) {
return void 0;
}
const result = [];
for (let index = 0; index < items.length; index += 2) {
const item = items[index];
result.push({
value: JSON.parse(item, (_k, v) => {
if (typeof v === "string" && dateMatch.test(v)) {
return new Date(v);
}
return v;
}),
score: Number(items[index + 1])
});
}
return result;
}
const items = await client.zrange(key, min, max);
if (!items) {
return void 0;
}
return items.map((/** @type {string} */ s) => JSON.parse(s, (_k, v) => {
if (typeof v === "string" && dateMatch.test(v)) {
return new Date(v);
}
return v;
}));
} finally {
if (client) {
await Connection.pool.release(client);
}
}
}
// MARK: static async getReverse
/**
* Retrieves data from a set in reverse order.
* @param {string} key The key to get the data for.
* @param {number} min The minimum index of the set.
* @param {number} max The maximum index of the set.
* @param {boolean} [withScores] Whether to include scores with the results.
* @returns {Promise<any[]>} A promise that returns the objects.
*/
static async getReverse(key, min, max, withScores) {
let client;
try {
client = await Connection.pool.acquire();
if (withScores) {
const items = await client.zrevrange(key, min, max, "WITHSCORES");
if (!items) {
return void 0;
}
const result = [];
for (let index = 0; index < items.length; index += 2) {
const item = items[index];
result.push({
value: JSON.parse(item, (_k, v) => {
if (typeof v === "string" && dateMatch.test(v)) {
return new Date(v);
}
return v;
}),
score: Number(items[index + 1])
});
}
return result;
}
const items = await client.zrevrange(key, min, max);
if (!items) {
return void 0;
}
return items.map((/** @type {string} */ s) => JSON.parse(s, (_k, v) => {
if (typeof v === "string" && dateMatch.test(v)) {
return new Date(v);
}
return v;
}));
} finally {
if (client) {
await Connection.pool.release(client);
}
}
}
// MARK: static async rank
/**
* Retrieves the rank of an item in a set.
* @param {string} key The key to get the data for.
* @param {any} member The member to get the data for.
* @returns {Promise<number>} A promise that returns the rank of the item in the set.
*/
static async rank(key, member) {
let client;
try {
client = await Connection.pool.acquire();
return client.zrank(key, JSON.stringify(member));
} finally {
if (client) {
await Connection.pool.release(client);
}
}
}
// MARK: static async rankReverse
/**
* Retrieves the reverse rank of an item in a set.
* @param {string} key The key to get the data for.
* @param {any} member The member to get the data for.
* @returns {Promise<number>} A promise that returns the rank of the item in the set.
*/
static async rankReverse(key, member) {
let client;
try {
client = await Connection.pool.acquire();
return client.zrevrank(key, JSON.stringify(member));
} finally {
if (client) {
await Connection.pool.release(client);
}
}
}
}
module.exports = SortedSetCache;