@boredland/node-ts-cache 5.0.1 → 5.1.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 +3 -2
- package/dist/cache-container/cache-container-types.d.ts +1 -1
- package/dist/cache-container/cache-container.d.ts +7 -1
- package/dist/cache-container/cache-container.js +12 -9
- package/dist/decorator/cache-decorator.js +1 -1
- package/dist/withCache.d.ts +2 -0
- package/dist/withCache.js +4 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
|
-
# boredland/node-ts-cache
|
|
1
|
+
# @boredland/node-ts-cache
|
|
2
2
|
|
|
3
3
|
[](https://github.com/boredland/node-ts-cache/actions/workflows/ci.yml)
|
|
4
4
|
[](http://opensource.org/licenses/MIT)
|
|
5
|
+
[](https://coveralls.io/github/boredland/node-ts-cache?branch=main)
|
|
5
6
|
|
|
6
7
|
Simple and extensible caching module supporting decorators.
|
|
7
8
|
|
|
8
9
|
## Install
|
|
9
10
|
|
|
10
11
|
```bash
|
|
11
|
-
yarn add boredland/node-ts-cache
|
|
12
|
+
yarn add @boredland/node-ts-cache
|
|
12
13
|
```
|
|
13
14
|
|
|
14
15
|
_Note: The underlying storage layer must be installed separately._
|
|
@@ -9,7 +9,7 @@ export declare type CachedItem<T = any> = {
|
|
|
9
9
|
export declare type CachingOptions = {
|
|
10
10
|
/** (Default: 60) Number of seconds to expire the cachte item */
|
|
11
11
|
ttl: number;
|
|
12
|
-
/** (Default: true) If true, expired cache entries will be deleted on touch. If false, entries will be deleted after the given ttl. */
|
|
12
|
+
/** (Default: true) If true, expired cache entries will be deleted on touch and returned anyway. If false, entries will be deleted after the given ttl. */
|
|
13
13
|
isLazy: boolean;
|
|
14
14
|
/** (Default: false) If true, cache entry has no expiration. */
|
|
15
15
|
isCachedForever: boolean;
|
|
@@ -3,7 +3,13 @@ import type { CachingOptions } from "./cache-container-types";
|
|
|
3
3
|
export declare class CacheContainer {
|
|
4
4
|
private storage;
|
|
5
5
|
constructor(storage: IStorage);
|
|
6
|
-
getItem<T>(key: string): Promise<
|
|
6
|
+
getItem<T>(key: string): Promise<{
|
|
7
|
+
content: T;
|
|
8
|
+
meta: {
|
|
9
|
+
expired: boolean;
|
|
10
|
+
createdAt: number;
|
|
11
|
+
};
|
|
12
|
+
} | undefined>;
|
|
7
13
|
setItem(key: string, content: any, options: Partial<CachingOptions>): Promise<void>;
|
|
8
14
|
clear(): Promise<void>;
|
|
9
15
|
private isItemExpired;
|
|
@@ -14,11 +14,20 @@ class CacheContainer {
|
|
|
14
14
|
}
|
|
15
15
|
async getItem(key) {
|
|
16
16
|
const item = await this.storage.getItem(key);
|
|
17
|
-
if (item
|
|
17
|
+
if (!item)
|
|
18
|
+
return;
|
|
19
|
+
const result = {
|
|
20
|
+
content: item.content,
|
|
21
|
+
meta: {
|
|
22
|
+
createdAt: item.meta.createdAt,
|
|
23
|
+
expired: this.isItemExpired(item)
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
if (result.meta.expired)
|
|
18
27
|
await this.unsetKey(key);
|
|
28
|
+
if (result.meta.expired && !item.meta.isLazy)
|
|
19
29
|
return undefined;
|
|
20
|
-
|
|
21
|
-
return item ? item.content : undefined;
|
|
30
|
+
return result;
|
|
22
31
|
}
|
|
23
32
|
async setItem(key, content, options) {
|
|
24
33
|
const finalOptions = {
|
|
@@ -32,12 +41,6 @@ class CacheContainer {
|
|
|
32
41
|
isLazy: finalOptions.isLazy,
|
|
33
42
|
ttl: finalOptions.isCachedForever ? Infinity : finalOptions.ttl * 1000
|
|
34
43
|
};
|
|
35
|
-
if (!finalOptions.isCachedForever && !finalOptions.isLazy) {
|
|
36
|
-
setTimeout(() => {
|
|
37
|
-
this.unsetKey(key);
|
|
38
|
-
debug(`Expired key ${key} removed from cache`);
|
|
39
|
-
}, meta.ttl);
|
|
40
|
-
}
|
|
41
44
|
await this.storage.setItem(key, { meta, content });
|
|
42
45
|
}
|
|
43
46
|
async clear() {
|
|
@@ -46,7 +46,7 @@ function Cache(container, options) {
|
|
|
46
46
|
const entry = await container.getItem(cacheKey);
|
|
47
47
|
if (entry) {
|
|
48
48
|
debug(`Cache HIT ${cacheKey}`);
|
|
49
|
-
return entry;
|
|
49
|
+
return entry.content;
|
|
50
50
|
}
|
|
51
51
|
debug(`Cache MISS ${cacheKey}`);
|
|
52
52
|
const methodResult = await runOriginalMethod();
|
package/dist/withCache.d.ts
CHANGED
|
@@ -4,6 +4,8 @@ declare type WithCacheOptions<Parameters> = Partial<Omit<CachingOptions, 'calcul
|
|
|
4
4
|
prefix?: string;
|
|
5
5
|
/** an optional function to calculate a key based on the parameters of the wrapped function */
|
|
6
6
|
calculateKey?: (input: Parameters) => string;
|
|
7
|
+
/** an optional function that is called when a lazy item has expired and thus got removed */
|
|
8
|
+
afterExpired?: () => Promise<void>;
|
|
7
9
|
};
|
|
8
10
|
/**
|
|
9
11
|
* wrapped function factory
|
package/dist/withCache.js
CHANGED
|
@@ -20,7 +20,10 @@ const withCacheFactory = (container) => {
|
|
|
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) {
|
|
23
|
-
|
|
23
|
+
if (cachedResponse.meta.expired && options.afterExpired) {
|
|
24
|
+
await options.afterExpired();
|
|
25
|
+
}
|
|
26
|
+
return cachedResponse.content;
|
|
24
27
|
}
|
|
25
28
|
const result = await operation(...parameters);
|
|
26
29
|
await container.setItem(key, result, rest);
|
package/package.json
CHANGED