-
Notifications
You must be signed in to change notification settings - Fork 201
Expand file tree
/
Copy pathstore.ts
More file actions
278 lines (248 loc) · 6.67 KB
/
store.ts
File metadata and controls
278 lines (248 loc) · 6.67 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
268
269
270
271
272
273
274
275
276
277
278
import { Stats, shorthandToMilliseconds } from "@cacheable/utils";
import { Hookified } from "hookified";
import type { PartialNodeCacheItem } from "index.js";
import Keyv from "keyv";
export type NodeCacheStoreOptions<T> = {
/**
* Time to live in milliseconds. This is a breaking change from the original NodeCache.
*/
ttl?: number | string;
/**
* Maximum number of keys to store in the cache. If this is set to a value greater than 0, the cache will keep track of the number of keys and will not store more than the specified number of keys.
*/
maxKeys?: number;
/**
* The Keyv store instance.
*/
store?: Keyv<T>;
/**
* Enable stats tracking. This is a breaking change from the original NodeCache.
*/
stats?: boolean;
};
export class NodeCacheStore<T> extends Hookified {
private _maxKeys = 0;
private _keyv: Keyv<T>;
private readonly _stats: Stats;
private _ttl?: number | string;
constructor(options?: NodeCacheStoreOptions<T>) {
super();
this._stats = new Stats({ enabled: options?.stats ?? true });
this._ttl = options?.ttl;
this._keyv = options?.store ?? new Keyv<T>();
if (options?.maxKeys) {
this._maxKeys = options.maxKeys;
}
// Hook up the keyv events
this._keyv.on("error", (error: Error) => {
/* v8 ignore next -- @preserve */
this.emit("error", error);
});
}
/**
* Time to live in milliseconds.
* @returns {number | string | undefined}
* @readonly
*/
public get ttl(): number | string | undefined {
return this._ttl;
}
/**
* Time to live in milliseconds.
* @param {number | string | undefined} ttl
*/
public set ttl(ttl: number | string | undefined) {
this._ttl = ttl;
}
/**
* The Keyv store instance.
* @returns {Keyv<T>}
* @readonly
*/
public get store(): Keyv<T> {
return this._keyv;
}
/**
* Maximum number of keys to store in the cache. if this is set to a value greater than 0,
* the cache will keep track of the number of keys and will not store more than the specified number of keys.
* @returns {number}
* @readonly
*/
public get maxKeys(): number {
return this._maxKeys;
}
/**
* Maximum number of keys to store in the cache. if this is set to a value greater than 0,
* the cache will keep track of the number of keys and will not store more than the specified number of keys.
* @param {number} maxKeys
*/
public set maxKeys(maxKeys: number) {
this._maxKeys = maxKeys;
/* v8 ignore next -- @preserve */
if (this._maxKeys > 0) {
this._stats.enabled = true;
}
}
/**
* Set a key/value pair in the cache.
* @param {string | number} key
* @param {T} value
* @param {number} [ttl]
* @returns {boolean}
*/
public async set(
key: string | number,
value: T,
ttl?: number | string,
): Promise<boolean> {
if (this._maxKeys > 0) {
if (this._stats.count >= this._maxKeys) {
return false;
}
}
const finalTtl = this.resolveTtl(ttl);
await this._keyv.set(key.toString(), value, finalTtl);
this._stats.incrementCount();
return true;
}
/**
* Set multiple key/value pairs in the cache.
* @param {PartialNodeCacheItem[]} list
* @returns {void}
*/
public async mset(list: Array<PartialNodeCacheItem<T>>): Promise<void> {
for (const item of list) {
const finalTtl = this.resolveTtl(item.ttl);
await this._keyv.set(item.key.toString(), item.value, finalTtl);
this._stats.incrementCount();
}
}
/**
* Get a value from the cache.
* @param {string | number} key
* @returns {any | undefined}
*/
public async get<V = T>(key: string | number): Promise<V | undefined> {
const result = await this._keyv.get<V>(key.toString());
if (result !== undefined) {
this._stats.incrementHits();
} else {
this._stats.incrementMisses();
}
return result;
}
/**
* Get multiple values from the cache.
* @param {Array<string | number>} keys
* @returns {Record<string, any | undefined>}
*/
public async mget<V = T>(
keys: Array<string | number>,
): Promise<Record<string, V | undefined>> {
const result: Record<string, V | undefined> = {};
for (const key of keys) {
const value = await this._keyv.get<V>(key.toString());
if (value !== undefined) {
this._stats.incrementHits();
} else {
this._stats.incrementMisses();
}
result[key.toString()] = value;
}
return result;
}
/**
* Delete a key from the cache.
* @param {string | number} key
* @returns {boolean}
*/
public async del(key: string | number): Promise<boolean> {
const deleted = await this._keyv.delete(key.toString());
if (deleted) {
this._stats.decreaseCount();
}
return deleted;
}
/**
* Delete multiple keys from the cache.
* @param {Array<string | number>} keys
* @returns {boolean}
*/
public async mdel(keys: Array<string | number>): Promise<boolean> {
const deleted = await this._keyv.delete(keys.map((key) => key.toString()));
if (deleted) {
for (const _ of keys) {
this._stats.decreaseCount();
}
}
return deleted;
}
/**
* Clear the cache.
* @returns {void}
*/
public async clear(): Promise<void> {
await this._keyv.clear();
this._stats.resetStoreValues();
}
/**
* Set the TTL of an existing key in the cache.
* @param {string | number} key
* @param {number | string} [ttl]
* @returns {boolean}
*/
public async setTtl(
key: string | number,
ttl?: number | string,
): Promise<boolean> {
const item = await this._keyv.get(key.toString());
if (item) {
const finalTtl = this.resolveTtl(ttl);
await this._keyv.set(key.toString(), item, finalTtl);
return true;
}
return false;
}
/**
* Get a key from the cache and delete it. If it does exist it will get the value and delete the item from the cache.
* @param {string | number} key
* @returns {T | undefined}
*/
public async take<V = T>(key: string | number): Promise<V | undefined> {
const result = await this._keyv.get<V>(key.toString());
if (result !== undefined) {
this._stats.incrementHits();
await this._keyv.delete(key.toString());
this._stats.decreaseCount();
} else {
this._stats.incrementMisses();
}
return result;
}
/**
* Disconnect from the cache.
* @returns {void}
*/
public async disconnect(): Promise<void> {
await this._keyv.disconnect();
}
/**
* Resolve the TTL to milliseconds.
* @param {number | string | undefined} ttl
* @returns {number | undefined}
*/
private resolveTtl(ttl?: number | string): number | undefined {
const effectiveTtl = ttl ?? this._ttl;
if (effectiveTtl === undefined) {
return undefined;
}
if (typeof effectiveTtl === "string") {
return shorthandToMilliseconds(effectiveTtl);
}
// Treat 0 as "unlimited" TTL; Keyv uses undefined to represent no expiration.
if (effectiveTtl === 0) {
return undefined;
}
return effectiveTtl;
}
}