@boredland/node-ts-cache 5.1.0 → 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/README.md +5 -4
- package/dist/cache-container/cache-container-types.d.ts +1 -1
- package/dist/cache-container/cache-container.d.ts +4 -4
- package/dist/cache-container/cache-container.js +6 -6
- package/dist/decorator/cache-decorator.js +8 -13
- package/dist/storage/storage-types.d.ts +3 -2
- package/dist/withCache.d.ts +1 -1
- package/dist/withCache.js +2 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -18,9 +18,10 @@ _Note: The underlying storage layer must be installed separately._
|
|
|
18
18
|
|
|
19
19
|
| Storage | Install |
|
|
20
20
|
|-----------------------------------------------------------------------|-------------------------------------------------|
|
|
21
|
-
| [memory](https://www.npmjs.com/package
|
|
22
|
-
| [node-fs](https://www.npmjs.com/package
|
|
23
|
-
| [ioredis](https://www.npmjs.com/package
|
|
21
|
+
| [memory](https://www.npmjs.com/package/@boredland/node-ts-cache-storage-memory)| ```yarn add @boredland/node-ts-cache-storage-memory```|
|
|
22
|
+
| [node-fs](https://www.npmjs.com/package/@boredland/node-ts-cache-storage-node-fs)| ```yarn add @boredland/node-ts-cache-storage-node-fs```|
|
|
23
|
+
| [ioredis](https://www.npmjs.com/package/@boredland/node-ts-cache-storage-ioredis)| ```yarn add @boredland/node-ts-cache-storage-ioredis```|
|
|
24
|
+
| [postgres](https://www.npmjs.com/package/@boredland/node-ts-cache-storage-pg)| ```yarn add @boredland/node-ts-cache-storage-pg```|
|
|
24
25
|
|
|
25
26
|
## Usage
|
|
26
27
|
|
|
@@ -71,7 +72,7 @@ const myCache = new CacheContainer(new MemoryStorage())
|
|
|
71
72
|
|
|
72
73
|
class MyService {
|
|
73
74
|
public async getUsers(): Promise<string[]> {
|
|
74
|
-
const cachedUsers = await myCache.getItem<string[]>("users")
|
|
75
|
+
const { content: cachedUsers } = await myCache.getItem<string[]>("users")
|
|
75
76
|
|
|
76
77
|
if (cachedUsers) {
|
|
77
78
|
return cachedUsers
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { Storage } from "../storage";
|
|
2
2
|
import type { CachingOptions } from "./cache-container-types";
|
|
3
3
|
export declare class CacheContainer {
|
|
4
4
|
private storage;
|
|
5
|
-
constructor(storage:
|
|
5
|
+
constructor(storage: Storage);
|
|
6
6
|
getItem<T>(key: string): Promise<{
|
|
7
7
|
content: T;
|
|
8
8
|
meta: {
|
|
@@ -10,8 +10,8 @@ export declare class CacheContainer {
|
|
|
10
10
|
createdAt: number;
|
|
11
11
|
};
|
|
12
12
|
} | undefined>;
|
|
13
|
-
setItem(key: string, content:
|
|
13
|
+
setItem(key: string, content: unknown, options?: Partial<CachingOptions>): Promise<void>;
|
|
14
14
|
clear(): Promise<void>;
|
|
15
15
|
private isItemExpired;
|
|
16
|
-
|
|
16
|
+
unsetKey(key: string): Promise<void>;
|
|
17
17
|
}
|
|
@@ -20,8 +20,8 @@ class CacheContainer {
|
|
|
20
20
|
content: item.content,
|
|
21
21
|
meta: {
|
|
22
22
|
createdAt: item.meta.createdAt,
|
|
23
|
-
expired: this.isItemExpired(item)
|
|
24
|
-
}
|
|
23
|
+
expired: this.isItemExpired(item),
|
|
24
|
+
},
|
|
25
25
|
};
|
|
26
26
|
if (result.meta.expired)
|
|
27
27
|
await this.unsetKey(key);
|
|
@@ -34,12 +34,12 @@ class CacheContainer {
|
|
|
34
34
|
ttl: DEFAULT_TTL_SECONDS,
|
|
35
35
|
isLazy: true,
|
|
36
36
|
isCachedForever: false,
|
|
37
|
-
...options
|
|
37
|
+
...options,
|
|
38
38
|
};
|
|
39
39
|
const meta = {
|
|
40
40
|
createdAt: Date.now(),
|
|
41
41
|
isLazy: finalOptions.isLazy,
|
|
42
|
-
ttl: finalOptions.isCachedForever ?
|
|
42
|
+
ttl: finalOptions.isCachedForever ? null : finalOptions.ttl * 1000,
|
|
43
43
|
};
|
|
44
44
|
await this.storage.setItem(key, { meta, content });
|
|
45
45
|
}
|
|
@@ -48,12 +48,12 @@ class CacheContainer {
|
|
|
48
48
|
debug("Cleared cache");
|
|
49
49
|
}
|
|
50
50
|
isItemExpired(item) {
|
|
51
|
-
if (item.meta.ttl ===
|
|
51
|
+
if (item.meta.ttl === null)
|
|
52
52
|
return false;
|
|
53
53
|
return Date.now() > item.meta.createdAt + item.meta.ttl;
|
|
54
54
|
}
|
|
55
55
|
async unsetKey(key) {
|
|
56
|
-
await this.storage.
|
|
56
|
+
await this.storage.removeItem(key);
|
|
57
57
|
}
|
|
58
58
|
}
|
|
59
59
|
exports.CacheContainer = CacheContainer;
|
|
@@ -10,7 +10,9 @@ const jsonCalculateKey = (data) => {
|
|
|
10
10
|
return `${data.className}:${data.methodName}:${JSON.stringify(data.args)}`;
|
|
11
11
|
};
|
|
12
12
|
function Cache(container, options) {
|
|
13
|
-
return function (target, methodName,
|
|
13
|
+
return function (target, methodName,
|
|
14
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
15
|
+
descriptor) {
|
|
14
16
|
const originalMethod = descriptor.value;
|
|
15
17
|
const className = target.constructor.name;
|
|
16
18
|
descriptor.value = async function (...args) {
|
|
@@ -18,21 +20,14 @@ function Cache(container, options) {
|
|
|
18
20
|
const keyOptions = {
|
|
19
21
|
args,
|
|
20
22
|
methodName: methodName,
|
|
21
|
-
className
|
|
23
|
+
className,
|
|
22
24
|
};
|
|
23
25
|
const cacheKey = options.calculateKey
|
|
24
26
|
? options.calculateKey(keyOptions)
|
|
25
27
|
: jsonCalculateKey(keyOptions);
|
|
26
|
-
const runOriginalMethod =
|
|
28
|
+
const runOriginalMethod = () => {
|
|
27
29
|
const methodCall = originalMethod.apply(this, args);
|
|
28
|
-
|
|
29
|
-
methodCall?.constructor?.name === "Promise";
|
|
30
|
-
if (isAsync) {
|
|
31
|
-
return await methodCall;
|
|
32
|
-
}
|
|
33
|
-
else {
|
|
34
|
-
return methodCall;
|
|
35
|
-
}
|
|
30
|
+
return methodCall;
|
|
36
31
|
};
|
|
37
32
|
if (!target.__node_ts_cache_method_run_queue) {
|
|
38
33
|
target.__node_ts_cache_method_run_queue = {};
|
|
@@ -54,8 +49,8 @@ function Cache(container, options) {
|
|
|
54
49
|
return methodResult;
|
|
55
50
|
}
|
|
56
51
|
finally {
|
|
57
|
-
|
|
58
|
-
|
|
52
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
53
|
+
target.__node_ts_cache_method_run_queue[cacheKey] = undefined;
|
|
59
54
|
}
|
|
60
55
|
})();
|
|
61
56
|
return target.__node_ts_cache_method_run_queue[cacheKey];
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { CachedItem } from "../cache-container";
|
|
2
|
-
export interface
|
|
2
|
+
export interface Storage {
|
|
3
3
|
getItem(key: string): Promise<CachedItem | undefined>;
|
|
4
|
-
setItem(key: string, content: CachedItem
|
|
4
|
+
setItem(key: string, content: CachedItem): Promise<void>;
|
|
5
|
+
removeItem(key: string): Promise<void>;
|
|
5
6
|
clear(): Promise<void>;
|
|
6
7
|
}
|
package/dist/withCache.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { CacheContainer, CachingOptions } from "./cache-container";
|
|
2
|
-
declare type WithCacheOptions<Parameters> = Partial<Omit<CachingOptions,
|
|
2
|
+
declare type WithCacheOptions<Parameters> = Partial<Omit<CachingOptions, "calculateKey">> & {
|
|
3
3
|
/** an optional prefix to prepend to the key */
|
|
4
4
|
prefix?: string;
|
|
5
5
|
/** an optional function to calculate a key based on the parameters of the wrapped function */
|
package/dist/withCache.js
CHANGED
|
@@ -15,8 +15,8 @@ const withCacheFactory = (container) => {
|
|
|
15
15
|
*/
|
|
16
16
|
const withCache = (operation, options = {}) => {
|
|
17
17
|
return async (...parameters) => {
|
|
18
|
-
|
|
19
|
-
prefix = prefix ??
|
|
18
|
+
const { calculateKey, ...rest } = options;
|
|
19
|
+
const prefix = options.prefix ?? "default";
|
|
20
20
|
const key = `${operation.name}:${prefix}:${calculateKey ? calculateKey(parameters) : JSON.stringify(parameters)}`;
|
|
21
21
|
const cachedResponse = await container.getItem(key);
|
|
22
22
|
if (cachedResponse) {
|
package/package.json
CHANGED