cacheable 0.3.0 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs ADDED
@@ -0,0 +1,448 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ Cacheable: () => Cacheable,
24
+ CacheableEvents: () => CacheableEvents,
25
+ CacheableHooks: () => CacheableHooks
26
+ });
27
+ module.exports = __toCommonJS(src_exports);
28
+ var import_keyv = require("keyv");
29
+ var import_hookified = require("hookified");
30
+
31
+ // src/stats.ts
32
+ var CacheableStats = class {
33
+ _hits = 0;
34
+ _misses = 0;
35
+ _gets = 0;
36
+ _sets = 0;
37
+ _deletes = 0;
38
+ _clears = 0;
39
+ _vsize = 0;
40
+ _ksize = 0;
41
+ _count = 0;
42
+ _enabled = false;
43
+ constructor(options) {
44
+ if (options?.enabled) {
45
+ this._enabled = options.enabled;
46
+ }
47
+ }
48
+ get enabled() {
49
+ return this._enabled;
50
+ }
51
+ set enabled(enabled) {
52
+ this._enabled = enabled;
53
+ }
54
+ get hits() {
55
+ return this._hits;
56
+ }
57
+ get misses() {
58
+ return this._misses;
59
+ }
60
+ get gets() {
61
+ return this._gets;
62
+ }
63
+ get sets() {
64
+ return this._sets;
65
+ }
66
+ get deletes() {
67
+ return this._deletes;
68
+ }
69
+ get clears() {
70
+ return this._clears;
71
+ }
72
+ get vsize() {
73
+ return this._vsize;
74
+ }
75
+ get ksize() {
76
+ return this._ksize;
77
+ }
78
+ get count() {
79
+ return this._count;
80
+ }
81
+ incrementHits() {
82
+ if (!this._enabled) {
83
+ return;
84
+ }
85
+ this._hits++;
86
+ }
87
+ incrementMisses() {
88
+ if (!this._enabled) {
89
+ return;
90
+ }
91
+ this._misses++;
92
+ }
93
+ incrementGets() {
94
+ if (!this._enabled) {
95
+ return;
96
+ }
97
+ this._gets++;
98
+ }
99
+ incrementSets() {
100
+ if (!this._enabled) {
101
+ return;
102
+ }
103
+ this._sets++;
104
+ }
105
+ incrementDeletes() {
106
+ if (!this._enabled) {
107
+ return;
108
+ }
109
+ this._deletes++;
110
+ }
111
+ incrementClears() {
112
+ if (!this._enabled) {
113
+ return;
114
+ }
115
+ this._clears++;
116
+ }
117
+ // eslint-disable-next-line @typescript-eslint/naming-convention
118
+ incrementVSize(value) {
119
+ if (!this._enabled) {
120
+ return;
121
+ }
122
+ this._vsize += this.roughSizeOfObject(value);
123
+ }
124
+ // eslint-disable-next-line @typescript-eslint/naming-convention
125
+ decreaseVSize(value) {
126
+ if (!this._enabled) {
127
+ return;
128
+ }
129
+ this._vsize -= this.roughSizeOfObject(value);
130
+ }
131
+ // eslint-disable-next-line @typescript-eslint/naming-convention
132
+ incrementKSize(key) {
133
+ if (!this._enabled) {
134
+ return;
135
+ }
136
+ this._ksize += this.roughSizeOfString(key);
137
+ }
138
+ // eslint-disable-next-line @typescript-eslint/naming-convention
139
+ decreaseKSize(key) {
140
+ if (!this._enabled) {
141
+ return;
142
+ }
143
+ this._ksize -= this.roughSizeOfString(key);
144
+ }
145
+ incrementCount() {
146
+ if (!this._enabled) {
147
+ return;
148
+ }
149
+ this._count++;
150
+ }
151
+ descreaseCount() {
152
+ if (!this._enabled) {
153
+ return;
154
+ }
155
+ this._count--;
156
+ }
157
+ setCount(count) {
158
+ if (!this._enabled) {
159
+ return;
160
+ }
161
+ this._count = count;
162
+ }
163
+ roughSizeOfString(value) {
164
+ return value.length * 2;
165
+ }
166
+ roughSizeOfObject(object) {
167
+ const objectList = [];
168
+ const stack = [object];
169
+ let bytes = 0;
170
+ while (stack.length > 0) {
171
+ const value = stack.pop();
172
+ if (typeof value === "boolean") {
173
+ bytes += 4;
174
+ } else if (typeof value === "string") {
175
+ bytes += value.length * 2;
176
+ } else if (typeof value === "number") {
177
+ bytes += 8;
178
+ } else if (typeof value === "object" && value !== null && !objectList.includes(value)) {
179
+ objectList.push(value);
180
+ for (const key in value) {
181
+ bytes += key.length * 2;
182
+ stack.push(value[key]);
183
+ }
184
+ }
185
+ }
186
+ return bytes;
187
+ }
188
+ reset() {
189
+ this._hits = 0;
190
+ this._misses = 0;
191
+ this._gets = 0;
192
+ this._sets = 0;
193
+ this._deletes = 0;
194
+ this._clears = 0;
195
+ this._vsize = 0;
196
+ this._ksize = 0;
197
+ this._count = 0;
198
+ }
199
+ };
200
+
201
+ // src/index.ts
202
+ var CacheableHooks = /* @__PURE__ */ ((CacheableHooks2) => {
203
+ CacheableHooks2["BEFORE_SET"] = "BEFORE_SET";
204
+ CacheableHooks2["AFTER_SET"] = "AFTER_SET";
205
+ CacheableHooks2["BEFORE_SET_MANY"] = "BEFORE_SET_MANY";
206
+ CacheableHooks2["AFTER_SET_MANY"] = "AFTER_SET_MANY";
207
+ CacheableHooks2["BEFORE_GET"] = "BEFORE_GET";
208
+ CacheableHooks2["AFTER_GET"] = "AFTER_GET";
209
+ CacheableHooks2["BEFORE_GET_MANY"] = "BEFORE_GET_MANY";
210
+ CacheableHooks2["AFTER_GET_MANY"] = "AFTER_GET_MANY";
211
+ return CacheableHooks2;
212
+ })(CacheableHooks || {});
213
+ var CacheableEvents = /* @__PURE__ */ ((CacheableEvents2) => {
214
+ CacheableEvents2["ERROR"] = "error";
215
+ return CacheableEvents2;
216
+ })(CacheableEvents || {});
217
+ var Cacheable = class extends import_hookified.Hookified {
218
+ _primary = new import_keyv.Keyv();
219
+ _secondary;
220
+ _nonBlocking = false;
221
+ _stats = new CacheableStats({ enabled: false });
222
+ constructor(options) {
223
+ super();
224
+ if (options?.primary) {
225
+ this.setPrimary(options.primary);
226
+ }
227
+ if (options?.secondary) {
228
+ this.setSecondary(options.secondary);
229
+ }
230
+ if (options?.nonBlocking) {
231
+ this._nonBlocking = options.nonBlocking;
232
+ }
233
+ if (options?.stats) {
234
+ this._stats.enabled = options.stats;
235
+ }
236
+ }
237
+ get stats() {
238
+ return this._stats;
239
+ }
240
+ get primary() {
241
+ return this._primary;
242
+ }
243
+ set primary(primary) {
244
+ this._primary = primary;
245
+ }
246
+ get secondary() {
247
+ return this._secondary;
248
+ }
249
+ set secondary(secondary) {
250
+ this._secondary = secondary;
251
+ }
252
+ get nonBlocking() {
253
+ return this._nonBlocking;
254
+ }
255
+ set nonBlocking(nonBlocking) {
256
+ this._nonBlocking = nonBlocking;
257
+ }
258
+ setPrimary(primary) {
259
+ this._primary = primary instanceof import_keyv.Keyv ? primary : new import_keyv.Keyv(primary);
260
+ }
261
+ setSecondary(secondary) {
262
+ this._secondary = secondary instanceof import_keyv.Keyv ? secondary : new import_keyv.Keyv(secondary);
263
+ }
264
+ async get(key) {
265
+ let result;
266
+ try {
267
+ await this.hook("BEFORE_GET" /* BEFORE_GET */, key);
268
+ result = await this._primary.get(key);
269
+ if (!result && this._secondary) {
270
+ result = await this._secondary.get(key);
271
+ if (result) {
272
+ await this._primary.set(key, result);
273
+ }
274
+ }
275
+ await this.hook("AFTER_GET" /* AFTER_GET */, { key, result });
276
+ } catch (error) {
277
+ await this.emit("error" /* ERROR */, error);
278
+ }
279
+ return result;
280
+ }
281
+ async getMany(keys) {
282
+ let result = [];
283
+ try {
284
+ await this.hook("BEFORE_GET_MANY" /* BEFORE_GET_MANY */, keys);
285
+ result = await this._primary.get(keys);
286
+ if (this._secondary) {
287
+ const missingKeys = [];
288
+ for (const [i, key] of keys.entries()) {
289
+ if (!result[i]) {
290
+ missingKeys.push(key);
291
+ }
292
+ }
293
+ const secondaryResult = await this._secondary.get(missingKeys);
294
+ for (const [i, key] of keys.entries()) {
295
+ if (!result[i] && secondaryResult[i]) {
296
+ result[i] = secondaryResult[i];
297
+ await this._primary.set(key, secondaryResult[i]);
298
+ }
299
+ }
300
+ }
301
+ await this.hook("AFTER_GET_MANY" /* AFTER_GET_MANY */, { keys, result });
302
+ } catch (error) {
303
+ await this.emit("error" /* ERROR */, error);
304
+ }
305
+ return result;
306
+ }
307
+ async set(key, value, ttl) {
308
+ let result = false;
309
+ try {
310
+ await this.hook("BEFORE_SET" /* BEFORE_SET */, { key, value, ttl });
311
+ const promises = [];
312
+ promises.push(this._primary.set(key, value, ttl));
313
+ if (this._secondary) {
314
+ promises.push(this._secondary.set(key, value, ttl));
315
+ }
316
+ if (this._nonBlocking) {
317
+ result = await Promise.race(promises);
318
+ } else {
319
+ const results = await Promise.all(promises);
320
+ result = results[0];
321
+ }
322
+ await this.hook("AFTER_SET" /* AFTER_SET */, { key, value, ttl });
323
+ } catch (error) {
324
+ await this.emit("error" /* ERROR */, error);
325
+ }
326
+ return result;
327
+ }
328
+ async setMany(items) {
329
+ let result = false;
330
+ try {
331
+ await this.hook("BEFORE_SET_MANY" /* BEFORE_SET_MANY */, items);
332
+ result = await this.setManyKeyv(this._primary, items);
333
+ if (this._secondary) {
334
+ if (this._nonBlocking) {
335
+ this.setManyKeyv(this._secondary, items);
336
+ } else {
337
+ await this.setManyKeyv(this._secondary, items);
338
+ }
339
+ }
340
+ await this.hook("AFTER_SET_MANY" /* AFTER_SET_MANY */, items);
341
+ } catch (error) {
342
+ await this.emit("error" /* ERROR */, error);
343
+ }
344
+ return result;
345
+ }
346
+ async take(key) {
347
+ const result = await this.get(key);
348
+ await this.delete(key);
349
+ return result;
350
+ }
351
+ async takeMany(keys) {
352
+ const result = await this.getMany(keys);
353
+ await this.deleteMany(keys);
354
+ return result;
355
+ }
356
+ async has(key) {
357
+ let result = await this._primary.has(key);
358
+ if (!result && this._secondary) {
359
+ result = await this._secondary.has(key);
360
+ }
361
+ return result;
362
+ }
363
+ async hasMany(keys) {
364
+ const result = await this.hasManyKeyv(this._primary, keys);
365
+ const missingKeys = [];
366
+ for (const [i, key] of keys.entries()) {
367
+ if (!result[i] && this._secondary) {
368
+ missingKeys.push(key);
369
+ }
370
+ }
371
+ if (missingKeys.length > 0 && this._secondary) {
372
+ const secondary = await this.hasManyKeyv(this._secondary, keys);
373
+ for (const [i, key] of keys.entries()) {
374
+ if (!result[i] && secondary[i]) {
375
+ result[i] = secondary[i];
376
+ }
377
+ }
378
+ }
379
+ return result;
380
+ }
381
+ async delete(key) {
382
+ const result = await this._primary.delete(key);
383
+ if (this._secondary) {
384
+ if (this._nonBlocking) {
385
+ this._secondary.delete(key);
386
+ } else {
387
+ await this._secondary.delete(key);
388
+ }
389
+ }
390
+ return result;
391
+ }
392
+ async deleteMany(keys) {
393
+ const result = await this.deleteManyKeyv(this._primary, keys);
394
+ if (this._secondary) {
395
+ if (this._nonBlocking) {
396
+ this.deleteManyKeyv(this._secondary, keys);
397
+ } else {
398
+ await this.deleteManyKeyv(this._secondary, keys);
399
+ }
400
+ }
401
+ return result;
402
+ }
403
+ async clear() {
404
+ const promises = [];
405
+ promises.push(this._primary.clear());
406
+ if (this._secondary) {
407
+ promises.push(this._secondary.clear());
408
+ }
409
+ await (this._nonBlocking ? Promise.race(promises) : Promise.all(promises));
410
+ }
411
+ async disconnect() {
412
+ const promises = [];
413
+ promises.push(this._primary.disconnect());
414
+ if (this._secondary) {
415
+ promises.push(this._secondary.disconnect());
416
+ }
417
+ await (this._nonBlocking ? Promise.race(promises) : Promise.all(promises));
418
+ }
419
+ async deleteManyKeyv(keyv, keys) {
420
+ const promises = [];
421
+ for (const key of keys) {
422
+ promises.push(keyv.delete(key));
423
+ }
424
+ await Promise.all(promises);
425
+ return true;
426
+ }
427
+ async setManyKeyv(keyv, items) {
428
+ const promises = [];
429
+ for (const item of items) {
430
+ promises.push(keyv.set(item.key, item.value, item.ttl));
431
+ }
432
+ await Promise.all(promises);
433
+ return true;
434
+ }
435
+ async hasManyKeyv(keyv, keys) {
436
+ const promises = [];
437
+ for (const key of keys) {
438
+ promises.push(keyv.has(key));
439
+ }
440
+ return Promise.all(promises);
441
+ }
442
+ };
443
+ // Annotate the CommonJS export names for ESM import in node:
444
+ 0 && (module.exports = {
445
+ Cacheable,
446
+ CacheableEvents,
447
+ CacheableHooks
448
+ });
@@ -0,0 +1,104 @@
1
+ import { Keyv, KeyvStoreAdapter } from 'keyv';
2
+ import { Hookified } from 'hookified';
3
+
4
+ type CacheableOptions$1 = {
5
+ enabled?: boolean;
6
+ };
7
+ declare class CacheableStats {
8
+ private _hits;
9
+ private _misses;
10
+ private _gets;
11
+ private _sets;
12
+ private _deletes;
13
+ private _clears;
14
+ private _vsize;
15
+ private _ksize;
16
+ private _count;
17
+ private _enabled;
18
+ constructor(options?: CacheableOptions$1);
19
+ get enabled(): boolean;
20
+ set enabled(enabled: boolean);
21
+ get hits(): number;
22
+ get misses(): number;
23
+ get gets(): number;
24
+ get sets(): number;
25
+ get deletes(): number;
26
+ get clears(): number;
27
+ get vsize(): number;
28
+ get ksize(): number;
29
+ get count(): number;
30
+ incrementHits(): void;
31
+ incrementMisses(): void;
32
+ incrementGets(): void;
33
+ incrementSets(): void;
34
+ incrementDeletes(): void;
35
+ incrementClears(): void;
36
+ incrementVSize(value: any): void;
37
+ decreaseVSize(value: any): void;
38
+ incrementKSize(key: string): void;
39
+ decreaseKSize(key: string): void;
40
+ incrementCount(): void;
41
+ descreaseCount(): void;
42
+ setCount(count: number): void;
43
+ roughSizeOfString(value: string): number;
44
+ roughSizeOfObject(object: any): number;
45
+ reset(): void;
46
+ }
47
+
48
+ declare enum CacheableHooks {
49
+ BEFORE_SET = "BEFORE_SET",
50
+ AFTER_SET = "AFTER_SET",
51
+ BEFORE_SET_MANY = "BEFORE_SET_MANY",
52
+ AFTER_SET_MANY = "AFTER_SET_MANY",
53
+ BEFORE_GET = "BEFORE_GET",
54
+ AFTER_GET = "AFTER_GET",
55
+ BEFORE_GET_MANY = "BEFORE_GET_MANY",
56
+ AFTER_GET_MANY = "AFTER_GET_MANY"
57
+ }
58
+ declare enum CacheableEvents {
59
+ ERROR = "error"
60
+ }
61
+ type CacheableItem = {
62
+ key: string;
63
+ value: unknown;
64
+ ttl?: number;
65
+ };
66
+ type CacheableOptions = {
67
+ primary?: Keyv | KeyvStoreAdapter;
68
+ secondary?: Keyv | KeyvStoreAdapter;
69
+ stats?: boolean;
70
+ nonBlocking?: boolean;
71
+ };
72
+ declare class Cacheable extends Hookified {
73
+ private _primary;
74
+ private _secondary;
75
+ private _nonBlocking;
76
+ private readonly _stats;
77
+ constructor(options?: CacheableOptions);
78
+ get stats(): CacheableStats;
79
+ get primary(): Keyv;
80
+ set primary(primary: Keyv);
81
+ get secondary(): Keyv | undefined;
82
+ set secondary(secondary: Keyv | undefined);
83
+ get nonBlocking(): boolean;
84
+ set nonBlocking(nonBlocking: boolean);
85
+ setPrimary(primary: Keyv | KeyvStoreAdapter): void;
86
+ setSecondary(secondary: Keyv | KeyvStoreAdapter): void;
87
+ get<T>(key: string): Promise<T | undefined>;
88
+ getMany<T>(keys: string[]): Promise<Array<T | undefined>>;
89
+ set<T>(key: string, value: T, ttl?: number): Promise<boolean>;
90
+ setMany(items: CacheableItem[]): Promise<boolean>;
91
+ take<T>(key: string): Promise<T | undefined>;
92
+ takeMany<T>(keys: string[]): Promise<Array<T | undefined>>;
93
+ has(key: string): Promise<boolean>;
94
+ hasMany(keys: string[]): Promise<boolean[]>;
95
+ delete(key: string): Promise<boolean>;
96
+ deleteMany(keys: string[]): Promise<boolean>;
97
+ clear(): Promise<void>;
98
+ disconnect(): Promise<void>;
99
+ private deleteManyKeyv;
100
+ private setManyKeyv;
101
+ private hasManyKeyv;
102
+ }
103
+
104
+ export { Cacheable, CacheableEvents, CacheableHooks, type CacheableItem, type CacheableOptions };
@@ -0,0 +1,104 @@
1
+ import { Keyv, KeyvStoreAdapter } from 'keyv';
2
+ import { Hookified } from 'hookified';
3
+
4
+ type CacheableOptions$1 = {
5
+ enabled?: boolean;
6
+ };
7
+ declare class CacheableStats {
8
+ private _hits;
9
+ private _misses;
10
+ private _gets;
11
+ private _sets;
12
+ private _deletes;
13
+ private _clears;
14
+ private _vsize;
15
+ private _ksize;
16
+ private _count;
17
+ private _enabled;
18
+ constructor(options?: CacheableOptions$1);
19
+ get enabled(): boolean;
20
+ set enabled(enabled: boolean);
21
+ get hits(): number;
22
+ get misses(): number;
23
+ get gets(): number;
24
+ get sets(): number;
25
+ get deletes(): number;
26
+ get clears(): number;
27
+ get vsize(): number;
28
+ get ksize(): number;
29
+ get count(): number;
30
+ incrementHits(): void;
31
+ incrementMisses(): void;
32
+ incrementGets(): void;
33
+ incrementSets(): void;
34
+ incrementDeletes(): void;
35
+ incrementClears(): void;
36
+ incrementVSize(value: any): void;
37
+ decreaseVSize(value: any): void;
38
+ incrementKSize(key: string): void;
39
+ decreaseKSize(key: string): void;
40
+ incrementCount(): void;
41
+ descreaseCount(): void;
42
+ setCount(count: number): void;
43
+ roughSizeOfString(value: string): number;
44
+ roughSizeOfObject(object: any): number;
45
+ reset(): void;
46
+ }
47
+
48
+ declare enum CacheableHooks {
49
+ BEFORE_SET = "BEFORE_SET",
50
+ AFTER_SET = "AFTER_SET",
51
+ BEFORE_SET_MANY = "BEFORE_SET_MANY",
52
+ AFTER_SET_MANY = "AFTER_SET_MANY",
53
+ BEFORE_GET = "BEFORE_GET",
54
+ AFTER_GET = "AFTER_GET",
55
+ BEFORE_GET_MANY = "BEFORE_GET_MANY",
56
+ AFTER_GET_MANY = "AFTER_GET_MANY"
57
+ }
58
+ declare enum CacheableEvents {
59
+ ERROR = "error"
60
+ }
61
+ type CacheableItem = {
62
+ key: string;
63
+ value: unknown;
64
+ ttl?: number;
65
+ };
66
+ type CacheableOptions = {
67
+ primary?: Keyv | KeyvStoreAdapter;
68
+ secondary?: Keyv | KeyvStoreAdapter;
69
+ stats?: boolean;
70
+ nonBlocking?: boolean;
71
+ };
72
+ declare class Cacheable extends Hookified {
73
+ private _primary;
74
+ private _secondary;
75
+ private _nonBlocking;
76
+ private readonly _stats;
77
+ constructor(options?: CacheableOptions);
78
+ get stats(): CacheableStats;
79
+ get primary(): Keyv;
80
+ set primary(primary: Keyv);
81
+ get secondary(): Keyv | undefined;
82
+ set secondary(secondary: Keyv | undefined);
83
+ get nonBlocking(): boolean;
84
+ set nonBlocking(nonBlocking: boolean);
85
+ setPrimary(primary: Keyv | KeyvStoreAdapter): void;
86
+ setSecondary(secondary: Keyv | KeyvStoreAdapter): void;
87
+ get<T>(key: string): Promise<T | undefined>;
88
+ getMany<T>(keys: string[]): Promise<Array<T | undefined>>;
89
+ set<T>(key: string, value: T, ttl?: number): Promise<boolean>;
90
+ setMany(items: CacheableItem[]): Promise<boolean>;
91
+ take<T>(key: string): Promise<T | undefined>;
92
+ takeMany<T>(keys: string[]): Promise<Array<T | undefined>>;
93
+ has(key: string): Promise<boolean>;
94
+ hasMany(keys: string[]): Promise<boolean[]>;
95
+ delete(key: string): Promise<boolean>;
96
+ deleteMany(keys: string[]): Promise<boolean>;
97
+ clear(): Promise<void>;
98
+ disconnect(): Promise<void>;
99
+ private deleteManyKeyv;
100
+ private setManyKeyv;
101
+ private hasManyKeyv;
102
+ }
103
+
104
+ export { Cacheable, CacheableEvents, CacheableHooks, type CacheableItem, type CacheableOptions };