@boredland/node-ts-cache 5.1.0 → 6.0.1

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 CHANGED
@@ -18,9 +18,11 @@ _Note: The underlying storage layer must be installed separately._
18
18
 
19
19
  | Storage | Install |
20
20
  |-----------------------------------------------------------------------|-------------------------------------------------|
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```|
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```|
25
+ | [elasticsearch](https://www.npmjs.com/package/@boredland/node-ts-cache-storage-elasticsearch)| ```yarn add @boredland/node-ts-cache-storage-elasticsearch```|
24
26
 
25
27
  ## Usage
26
28
 
@@ -71,7 +73,7 @@ const myCache = new CacheContainer(new MemoryStorage())
71
73
 
72
74
  class MyService {
73
75
  public async getUsers(): Promise<string[]> {
74
- const cachedUsers = await myCache.getItem<string[]>("users")
76
+ const { content: cachedUsers } = await myCache.getItem<string[]>("users")
75
77
 
76
78
  if (cachedUsers) {
77
79
  return cachedUsers
@@ -2,7 +2,7 @@ export declare type CachedItem<T = any> = {
2
2
  content: T;
3
3
  meta: {
4
4
  createdAt: number;
5
- ttl: number;
5
+ ttl: number | null;
6
6
  isLazy: boolean;
7
7
  };
8
8
  };
@@ -1,8 +1,8 @@
1
- import type { IStorage } from "../storage";
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: IStorage);
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: any, options: Partial<CachingOptions>): Promise<void>;
13
+ setItem(key: string, content: unknown, options?: Partial<CachingOptions>): Promise<void>;
14
14
  clear(): Promise<void>;
15
15
  private isItemExpired;
16
- private unsetKey;
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 ? Infinity : finalOptions.ttl * 1000
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 === Infinity)
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.setItem(key, undefined);
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, descriptor) {
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 = async () => {
28
+ const runOriginalMethod = () => {
27
29
  const methodCall = originalMethod.apply(this, args);
28
- const isAsync = methodCall?.constructor?.name === "AsyncFunction" ||
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
- target.__node_ts_cache_method_run_queue[cacheKey] =
58
- undefined;
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 IStorage {
2
+ export interface Storage {
3
3
  getItem(key: string): Promise<CachedItem | undefined>;
4
- setItem(key: string, content: CachedItem | undefined): Promise<void>;
4
+ setItem(key: string, content: CachedItem): Promise<void>;
5
+ removeItem(key: string): Promise<void>;
5
6
  clear(): Promise<void>;
6
7
  }
@@ -1,5 +1,5 @@
1
1
  import type { CacheContainer, CachingOptions } from "./cache-container";
2
- declare type WithCacheOptions<Parameters> = Partial<Omit<CachingOptions, 'calculateKey'>> & {
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
- let { prefix, calculateKey, ...rest } = options;
19
- prefix = prefix ?? 'default';
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
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@boredland/node-ts-cache",
3
3
  "description": "Simple and extensible caching module supporting decorators",
4
- "version": "5.1.0",
4
+ "version": "6.0.1",
5
5
  "private": false,
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",