@boredland/node-ts-cache 4.4.0 → 5.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
@@ -17,29 +17,32 @@ _Note: The underlying storage layer must be installed separately._
17
17
 
18
18
  | Storage | Install |
19
19
  |-----------------------------------------------------------------------|-------------------------------------------------|
20
- | [memory](https://www.npmjs.com/package/boredland/node-ts-cache-storage-memory)| ```yarn add boredland/node-ts-cache-storage-memory```|
21
- | [node-fs](https://www.npmjs.com/package/boredland/node-ts-cache-storage-node-fs)| ```yarn add boredland/node-ts-cache-storage-node-fs```|
22
- | [ioredis](https://www.npmjs.com/package/boredland/node-ts-cache-storage-ioredis)| ```yarn add boredland/node-ts-cache-storage-ioredis```|
20
+ | [memory](https://www.npmjs.com/package/boredland/node-ts-cache-storage-memory)| ```yarn add @boredland/node-ts-cache-storage-memory```|
21
+ | [node-fs](https://www.npmjs.com/package/boredland/node-ts-cache-storage-node-fs)| ```yarn add @boredland/node-ts-cache-storage-node-fs```|
22
+ | [ioredis](https://www.npmjs.com/package/boredland/node-ts-cache-storage-ioredis)| ```yarn add @boredland/node-ts-cache-storage-ioredis```|
23
23
 
24
24
  ## Usage
25
25
 
26
- ## With decorator
26
+ ### withCacheFactory
27
27
 
28
- Caches function response using the given options.
29
- Works with the above listed storages.
30
- By default, uses all arguments to build an unique key.
28
+ Function wrapper factory for arbitrary functions. The cache key is caculated based on the parameters passed to the function.
31
29
 
32
- `@Cache(container, options)`
30
+ ```ts
31
+ import { withCacheFactory, CacheContainer } from '@boredland/node-ts-cache'
32
+ import { MemoryStorage } from '@boredland/node-ts-cache-storage-memory'
33
+
34
+ const doThingsCache = new CacheContainer(new MemoryStorage())
35
+
36
+ const someFn = (input: { a: string, b: number })
37
+
38
+ const wrappedFn = withCacheFactory(doThingsCache)(someFn);
39
+
40
+ const result = someFn({ a: "lala", b: 123 })
41
+ ```
42
+
43
+ ### With decorator
33
44
 
34
- - `options`:
35
- - `ttl`: _(Default: 60)_ Number of seconds to expire the cachte item
36
- - `isLazy`: _(Default: true)_ If true, expired cache entries will be deleted on touch. If false, entries will be deleted after the given _ttl_.
37
- - `isCachedForever`: _(Default: false)_ If true, cache entry has no expiration.
38
- - `calculateKey(data => string)`: _(Default: JSON.stringify combination of className, methodName and call args)_
39
- - `data`:
40
- - `className`: The class name for the method being decorated
41
- - `methodName`: The method name being decorated
42
- - `args`: The arguments passed to the method when called
45
+ Caches function response using the given options. By default, uses all arguments to build an unique key.
43
46
 
44
47
  _Note: @Cache will consider the return type of the function. If the return type is a thenable, it will stay that way, otherwise not._
45
48
 
@@ -57,7 +60,7 @@ class MyService {
57
60
  }
58
61
  ```
59
62
 
60
- ## Directly
63
+ ### Direct usage
61
64
 
62
65
  ```ts
63
66
  import { CacheContainer } from '@boredland/node-ts-cache'
@@ -7,12 +7,19 @@ export declare type CachedItem<T = any> = {
7
7
  };
8
8
  };
9
9
  export declare type CachingOptions = {
10
+ /** (Default: 60) Number of seconds to expire the cachte item */
10
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. */
11
13
  isLazy: boolean;
14
+ /** (Default: false) If true, cache entry has no expiration. */
12
15
  isCachedForever: boolean;
16
+ /** (Default: JSON.stringify combination of className, methodName and call args) */
13
17
  calculateKey: (data: {
18
+ /** The class name for the method being decorated */
14
19
  className: string;
20
+ /** The method name being decorated */
15
21
  methodName: string;
22
+ /** The arguments passed to the method when called */
16
23
  args: any[];
17
24
  }) => string;
18
25
  };
@@ -1,12 +1,14 @@
1
1
  import type { CacheContainer, CachingOptions } from "./cache-container";
2
2
  declare type WithCacheOptions<Parameters> = Partial<Omit<CachingOptions, 'calculateKey'>> & {
3
- prefix: string;
4
- calculateKey?: (prefix: string, input: Parameters) => string;
3
+ /** an optional prefix to prepend to the key */
4
+ prefix?: string;
5
+ /** an optional function to calculate a key based on the parameters of the wrapped function */
6
+ calculateKey?: (input: Parameters) => string;
5
7
  };
6
8
  /**
7
9
  * wrapped function factory
8
10
  * @param container - cache container to create the fn for
9
- * @returns a wrapped function
11
+ * @returns wrapping function
10
12
  */
11
- export declare const withCacheFactory: (container: CacheContainer) => <Parameters_1 extends unknown[], Result extends Promise<unknown>>(operation: (...parameters: Parameters_1) => Result, { calculateKey, prefix, ...options }: WithCacheOptions<Parameters_1>) => (...parameters: Parameters_1) => Promise<Result>;
13
+ export declare const withCacheFactory: (container: CacheContainer) => <Parameters_1 extends unknown[], Result extends Promise<unknown>>(operation: (...parameters: Parameters_1) => Result, options?: WithCacheOptions<Parameters_1>) => (...parameters: Parameters_1) => Promise<Result>;
12
14
  export {};
package/dist/withCache.js CHANGED
@@ -4,18 +4,26 @@ exports.withCacheFactory = void 0;
4
4
  /**
5
5
  * wrapped function factory
6
6
  * @param container - cache container to create the fn for
7
- * @returns a wrapped function
7
+ * @returns wrapping function
8
8
  */
9
9
  const withCacheFactory = (container) => {
10
- const withCache = (operation, { calculateKey, prefix, ...options }) => {
10
+ /**
11
+ * function wrapper
12
+ * @param operation - the function to be wrapped
13
+ * @param options - caching options
14
+ * @returns wrapped operation
15
+ */
16
+ const withCache = (operation, options = {}) => {
11
17
  return async (...parameters) => {
12
- const key = calculateKey ? calculateKey(prefix, parameters) : `${prefix}_${JSON.stringify(parameters)}`;
18
+ let { prefix, calculateKey, ...rest } = options;
19
+ prefix = prefix ?? 'default';
20
+ const key = `${operation.name}:${prefix}:${calculateKey ? calculateKey(parameters) : JSON.stringify(parameters)}`;
13
21
  const cachedResponse = await container.getItem(key);
14
22
  if (cachedResponse) {
15
23
  return cachedResponse;
16
24
  }
17
25
  const result = await operation(...parameters);
18
- await container.setItem(key, result, options);
26
+ await container.setItem(key, result, rest);
19
27
  return result;
20
28
  };
21
29
  };
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": "4.4.0",
4
+ "version": "5.0.1",
5
5
  "private": false,
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",
@@ -32,7 +32,6 @@
32
32
  "node-cache",
33
33
  "ts-cache"
34
34
  ],
35
- "author": "Himmet Avsar <avsar.himmet1@gmail.com>",
36
35
  "license": "MIT",
37
36
  "bugs": {
38
37
  "url": "https://github.com/boredland/node-ts-cache/issues"
package/dist/hasher.d.ts DELETED
@@ -1,3 +0,0 @@
1
- import objectHash from 'node-object-hash';
2
- declare const hash: (object: any, opts?: objectHash.HasherOptions | undefined) => string;
3
- export { hash };
package/dist/hasher.js DELETED
@@ -1,9 +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.hash = void 0;
7
- const node_object_hash_1 = __importDefault(require("node-object-hash"));
8
- const { hash } = (0, node_object_hash_1.default)();
9
- exports.hash = hash;