cache-manager 5.7.5 → 6.0.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/LICENSE +1 -1
- package/README.md +287 -240
- package/dist/index.cjs +294 -0
- package/dist/index.d.cts +76 -0
- package/dist/index.d.ts +76 -3
- package/dist/index.js +257 -19
- package/package.json +43 -48
- package/dist/caching.d.ts +0 -60
- package/dist/caching.js +0 -99
- package/dist/multi-caching.d.ts +0 -6
- package/dist/multi-caching.js +0 -129
- package/dist/stores/index.d.ts +0 -1
- package/dist/stores/index.js +0 -18
- package/dist/stores/memory.d.ts +0 -22
- package/dist/stores/memory.js +0 -90
- package/dist/utils.d.ts +0 -8
- package/dist/utils.js +0 -20
package/dist/multi-caching.js
DELETED
|
@@ -1,129 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.multiCaching = multiCaching;
|
|
7
|
-
const eventemitter3_1 = __importDefault(require("eventemitter3"));
|
|
8
|
-
const caching_js_1 = require("./caching.js");
|
|
9
|
-
const utils_js_1 = require("./utils.js");
|
|
10
|
-
/**
|
|
11
|
-
* Module that lets you specify a hierarchy of caches.
|
|
12
|
-
*/
|
|
13
|
-
function multiCaching(caches) {
|
|
14
|
-
const eventEmitter = new eventemitter3_1.default();
|
|
15
|
-
for (const cache of caches) {
|
|
16
|
-
cache.on('error', event => eventEmitter.emit('error', event));
|
|
17
|
-
}
|
|
18
|
-
const get = async (key) => {
|
|
19
|
-
for (const cache of caches) {
|
|
20
|
-
try {
|
|
21
|
-
// eslint-disable-next-line no-await-in-loop
|
|
22
|
-
const value = await cache.get(key);
|
|
23
|
-
if (value !== undefined) {
|
|
24
|
-
return value;
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
catch (error) {
|
|
28
|
-
const errorEvent = { error, key, operation: 'get' };
|
|
29
|
-
eventEmitter.emit('error', errorEvent);
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
};
|
|
33
|
-
const set = async (key, data, ttl, slice = caches.length) => {
|
|
34
|
-
await Promise.all(caches.slice(0, slice).map(async (cache) => cache.set(key, data, ttl))).catch(error => {
|
|
35
|
-
const errorEvent = {
|
|
36
|
-
error, key, operation: 'set', data,
|
|
37
|
-
};
|
|
38
|
-
eventEmitter.emit('error', errorEvent);
|
|
39
|
-
});
|
|
40
|
-
};
|
|
41
|
-
return {
|
|
42
|
-
get,
|
|
43
|
-
async set(key, value, ttl) {
|
|
44
|
-
await set(key, value, ttl);
|
|
45
|
-
},
|
|
46
|
-
async del(key) {
|
|
47
|
-
await Promise.all(caches.map(async (cache) => cache.del(key))).catch(error => {
|
|
48
|
-
const errorEvent = { error, key, operation: 'del' };
|
|
49
|
-
eventEmitter.emit('error', errorEvent);
|
|
50
|
-
});
|
|
51
|
-
},
|
|
52
|
-
// eslint-disable-next-line max-params
|
|
53
|
-
async wrap(key, function_, ttl, refreshThreshold, options = {}) {
|
|
54
|
-
const options_ = { ...caching_js_1.defaultWrapOptions, ...options };
|
|
55
|
-
let value;
|
|
56
|
-
let i = 0;
|
|
57
|
-
for (; i < caches.length; i++) {
|
|
58
|
-
try {
|
|
59
|
-
// eslint-disable-next-line no-await-in-loop
|
|
60
|
-
value = await caches[i].get(key);
|
|
61
|
-
if (value !== undefined) {
|
|
62
|
-
break;
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
catch (error) {
|
|
66
|
-
const errorEvent = { error, key, operation: 'get' };
|
|
67
|
-
eventEmitter.emit('error', errorEvent);
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
if (value === undefined) {
|
|
71
|
-
const result = await function_();
|
|
72
|
-
const cacheTtl = typeof ttl === 'function' ? ttl(result) : ttl;
|
|
73
|
-
await (0, utils_js_1.conditionalAwait)(async () => set(key, result, cacheTtl), !options_.nonBlockingSet);
|
|
74
|
-
return result;
|
|
75
|
-
}
|
|
76
|
-
const cacheTtl = typeof ttl === 'function' ? ttl(value) : ttl;
|
|
77
|
-
await (0, utils_js_1.conditionalAwait)(async () => set(key, value, cacheTtl, i).then(async () => caches[i].wrap(key, function_, ttl, refreshThreshold)), !options_.nonBlockingSet);
|
|
78
|
-
return value;
|
|
79
|
-
},
|
|
80
|
-
async reset() {
|
|
81
|
-
await Promise.all(caches.map(async (x) => x.reset())).catch(error => {
|
|
82
|
-
const errorEvent = { error, key: '', operation: 'reset' };
|
|
83
|
-
eventEmitter.emit('error', errorEvent);
|
|
84
|
-
});
|
|
85
|
-
},
|
|
86
|
-
async mget(...keys) {
|
|
87
|
-
const values = Array.from({ length: keys.length }).fill(undefined);
|
|
88
|
-
for (const cache of caches) {
|
|
89
|
-
if (values.every(x => x !== undefined)) {
|
|
90
|
-
break;
|
|
91
|
-
}
|
|
92
|
-
try {
|
|
93
|
-
// eslint-disable-next-line no-await-in-loop
|
|
94
|
-
const value = await cache.store.mget(...keys);
|
|
95
|
-
for (const [i, v] of value.entries()) {
|
|
96
|
-
if (values[i] === undefined && v !== undefined) {
|
|
97
|
-
values[i] = v;
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
catch (error) {
|
|
102
|
-
const errorEvent = { error, keys, operation: 'mget' };
|
|
103
|
-
eventEmitter.emit('error', errorEvent);
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
return values;
|
|
107
|
-
},
|
|
108
|
-
async mset(arguments_, ttl) {
|
|
109
|
-
await Promise.all(caches.map(async (cache) => cache.store.mset(arguments_, ttl))).catch(error => {
|
|
110
|
-
const keys = arguments_.map(([key]) => key);
|
|
111
|
-
const data = arguments_.map(([, value]) => value);
|
|
112
|
-
const errorEvent = {
|
|
113
|
-
error, keys, data, operation: 'mset',
|
|
114
|
-
};
|
|
115
|
-
eventEmitter.emit('error', errorEvent);
|
|
116
|
-
});
|
|
117
|
-
},
|
|
118
|
-
async mdel(...keys) {
|
|
119
|
-
await Promise.all(caches.map(async (cache) => cache.store.mdel(...keys)))
|
|
120
|
-
.catch(error => {
|
|
121
|
-
const errorEvent = { error, keys, operation: 'mdel' };
|
|
122
|
-
eventEmitter.emit('error', errorEvent);
|
|
123
|
-
});
|
|
124
|
-
},
|
|
125
|
-
on: (event, handler) => eventEmitter.on('error', handler),
|
|
126
|
-
removeListener: (event, handler) => eventEmitter.removeListener(event, handler),
|
|
127
|
-
};
|
|
128
|
-
}
|
|
129
|
-
//# sourceMappingURL=multi-caching.js.map
|
package/dist/stores/index.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './memory.js';
|
package/dist/stores/index.js
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./memory.js"), exports);
|
|
18
|
-
//# sourceMappingURL=index.js.map
|
package/dist/stores/memory.d.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { LRUCache } from 'lru-cache';
|
|
2
|
-
import { type Config, type Cache, type Store } from '../caching.js';
|
|
3
|
-
type LRU = LRUCache<string, any>;
|
|
4
|
-
type Pre = LRUCache.OptionsTTLLimit<string, any, unknown>;
|
|
5
|
-
type Options = Omit<Pre, 'ttlAutopurge'> & Partial<Pick<Pre, 'ttlAutopurge'>>;
|
|
6
|
-
export type MemoryConfig = {
|
|
7
|
-
max?: number;
|
|
8
|
-
sizeCalculation?: (value: unknown, key: string) => number;
|
|
9
|
-
shouldCloneBeforeSet?: boolean;
|
|
10
|
-
} & Options & Config;
|
|
11
|
-
export type MemoryStore = Store & {
|
|
12
|
-
dump: LRU['dump'];
|
|
13
|
-
load: LRU['load'];
|
|
14
|
-
calculatedSize: LRU['calculatedSize'];
|
|
15
|
-
get size(): number;
|
|
16
|
-
};
|
|
17
|
-
export type MemoryCache = Cache<MemoryStore>;
|
|
18
|
-
/**
|
|
19
|
-
* Wrapper for lru-cache.
|
|
20
|
-
*/
|
|
21
|
-
export declare function memoryStore(arguments_?: MemoryConfig): MemoryStore;
|
|
22
|
-
export {};
|
package/dist/stores/memory.js
DELETED
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.memoryStore = memoryStore;
|
|
7
|
-
const lru_cache_1 = require("lru-cache");
|
|
8
|
-
const lodash_clonedeep_1 = __importDefault(require("lodash.clonedeep"));
|
|
9
|
-
function clone(object) {
|
|
10
|
-
if (typeof object === 'object' && object !== null) {
|
|
11
|
-
return (0, lodash_clonedeep_1.default)(object);
|
|
12
|
-
}
|
|
13
|
-
return object;
|
|
14
|
-
}
|
|
15
|
-
/**
|
|
16
|
-
* Wrapper for lru-cache.
|
|
17
|
-
*/
|
|
18
|
-
function memoryStore(arguments_) {
|
|
19
|
-
const shouldCloneBeforeSet = arguments_?.shouldCloneBeforeSet !== false; // Clone by default
|
|
20
|
-
const isCacheable = arguments_?.isCacheable ?? (value => value !== undefined);
|
|
21
|
-
const lruOptions = {
|
|
22
|
-
ttlAutopurge: true,
|
|
23
|
-
...arguments_,
|
|
24
|
-
max: arguments_?.max ?? 500,
|
|
25
|
-
ttl: arguments_?.ttl === undefined ? 0 : arguments_.ttl,
|
|
26
|
-
};
|
|
27
|
-
const lruCache = new lru_cache_1.LRUCache(lruOptions);
|
|
28
|
-
return {
|
|
29
|
-
async del(key) {
|
|
30
|
-
lruCache.delete(key);
|
|
31
|
-
},
|
|
32
|
-
get: async (key) => lruCache.get(key),
|
|
33
|
-
keys: async () => [...lruCache.keys()],
|
|
34
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
35
|
-
mget: async (...arguments_) => arguments_.map(x => lruCache.get(x)),
|
|
36
|
-
async mset(arguments_, ttl) {
|
|
37
|
-
const opt = { ttl: ttl ?? lruOptions.ttl };
|
|
38
|
-
for (const [key, value] of arguments_) {
|
|
39
|
-
if (!isCacheable(value)) {
|
|
40
|
-
throw new Error(`no cacheable value ${JSON.stringify(value)}`);
|
|
41
|
-
}
|
|
42
|
-
if (shouldCloneBeforeSet) {
|
|
43
|
-
lruCache.set(key, clone(value), opt);
|
|
44
|
-
}
|
|
45
|
-
else {
|
|
46
|
-
lruCache.set(key, value, opt);
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
},
|
|
50
|
-
async mdel(...arguments_) {
|
|
51
|
-
for (const key of arguments_) {
|
|
52
|
-
lruCache.delete(key);
|
|
53
|
-
}
|
|
54
|
-
},
|
|
55
|
-
async reset() {
|
|
56
|
-
lruCache.clear();
|
|
57
|
-
},
|
|
58
|
-
ttl: async (key) => lruCache.getRemainingTTL(key),
|
|
59
|
-
async set(key, value, opt) {
|
|
60
|
-
if (!isCacheable(value)) {
|
|
61
|
-
throw new Error(`no cacheable value ${JSON.stringify(value)}`);
|
|
62
|
-
}
|
|
63
|
-
if (shouldCloneBeforeSet) {
|
|
64
|
-
value = clone(value);
|
|
65
|
-
}
|
|
66
|
-
const ttl = opt ?? lruOptions.ttl;
|
|
67
|
-
lruCache.set(key, value, { ttl });
|
|
68
|
-
},
|
|
69
|
-
get calculatedSize() {
|
|
70
|
-
return lruCache.calculatedSize;
|
|
71
|
-
},
|
|
72
|
-
/**
|
|
73
|
-
* This method is not available in the caching modules.
|
|
74
|
-
*/
|
|
75
|
-
get size() {
|
|
76
|
-
return lruCache.size;
|
|
77
|
-
},
|
|
78
|
-
/**
|
|
79
|
-
* This method is not available in the caching modules.
|
|
80
|
-
*/
|
|
81
|
-
dump: () => lruCache.dump(),
|
|
82
|
-
/**
|
|
83
|
-
* This method is not available in the caching modules.
|
|
84
|
-
*/
|
|
85
|
-
load(...arguments_) {
|
|
86
|
-
lruCache.load(...arguments_);
|
|
87
|
-
},
|
|
88
|
-
};
|
|
89
|
-
}
|
|
90
|
-
//# sourceMappingURL=memory.js.map
|
package/dist/utils.d.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* If blocking is true, it will await the function before returning.
|
|
3
|
-
* Otherwise, it will fire the execution and return before it ends.
|
|
4
|
-
* Consumer of this function should await the returned promise.
|
|
5
|
-
* @param function_
|
|
6
|
-
* @param blocking
|
|
7
|
-
*/
|
|
8
|
-
export declare const conditionalAwait: (function_: () => Promise<unknown>, blocking: boolean) => Promise<void>;
|
package/dist/utils.js
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.conditionalAwait = void 0;
|
|
4
|
-
/**
|
|
5
|
-
* If blocking is true, it will await the function before returning.
|
|
6
|
-
* Otherwise, it will fire the execution and return before it ends.
|
|
7
|
-
* Consumer of this function should await the returned promise.
|
|
8
|
-
* @param function_
|
|
9
|
-
* @param blocking
|
|
10
|
-
*/
|
|
11
|
-
const conditionalAwait = async (function_, blocking) => {
|
|
12
|
-
if (blocking) {
|
|
13
|
-
await function_();
|
|
14
|
-
}
|
|
15
|
-
else {
|
|
16
|
-
void function_().then();
|
|
17
|
-
}
|
|
18
|
-
};
|
|
19
|
-
exports.conditionalAwait = conditionalAwait;
|
|
20
|
-
//# sourceMappingURL=utils.js.map
|