@boredland/node-ts-cache 6.0.0 → 6.0.2

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
@@ -22,6 +22,7 @@ _Note: The underlying storage layer must be installed separately._
22
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
23
  | [ioredis](https://www.npmjs.com/package/@boredland/node-ts-cache-storage-ioredis)| ```yarn add @boredland/node-ts-cache-storage-ioredis```|
24
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```|
25
26
 
26
27
  ## Usage
27
28
 
@@ -1,3 +1,4 @@
1
+ import type { Storage } from "./storage";
1
2
  export declare type CachedItem<T = any> = {
2
3
  content: T;
3
4
  meta: {
@@ -23,3 +24,18 @@ export declare type CachingOptions = {
23
24
  args: any[];
24
25
  }) => string;
25
26
  };
27
+ export declare class CacheContainer {
28
+ private storage;
29
+ constructor(storage: Storage);
30
+ getItem<T>(key: string): Promise<{
31
+ content: T;
32
+ meta: {
33
+ expired: boolean;
34
+ createdAt: number;
35
+ };
36
+ } | undefined>;
37
+ setItem(key: string, content: unknown, options?: Partial<CachingOptions>): Promise<void>;
38
+ clear(): Promise<void>;
39
+ private isItemExpired;
40
+ unsetKey(key: string): Promise<void>;
41
+ }
@@ -0,0 +1,8 @@
1
+ import type { CacheContainer, CachingOptions } from "./cacheContainer";
2
+ /**
3
+ * Decorator to be used on class methods to be cached
4
+ * @param container - container instance to be used for the method
5
+ * @param options - caching options to be used for the method
6
+ * @returns the return value of the method, cached or otherwise
7
+ */
8
+ export declare function Cache(container: CacheContainer, options?: Partial<CachingOptions>): MethodDecorator;
@@ -9,6 +9,12 @@ const debug = (0, debug_1.default)("node-ts-cache");
9
9
  const jsonCalculateKey = (data) => {
10
10
  return `${data.className}:${data.methodName}:${JSON.stringify(data.args)}`;
11
11
  };
12
+ /**
13
+ * Decorator to be used on class methods to be cached
14
+ * @param container - container instance to be used for the method
15
+ * @param options - caching options to be used for the method
16
+ * @returns the return value of the method, cached or otherwise
17
+ */
12
18
  function Cache(container, options) {
13
19
  return function (target, methodName,
14
20
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  export * from "./decorator";
2
2
  export * from "./storage";
3
- export * from "./cache-container";
3
+ export * from "./cacheContainer";
4
4
  export * from "./withCache";
package/dist/index.js CHANGED
@@ -16,5 +16,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./decorator"), exports);
18
18
  __exportStar(require("./storage"), exports);
19
- __exportStar(require("./cache-container"), exports);
19
+ __exportStar(require("./cacheContainer"), exports);
20
20
  __exportStar(require("./withCache"), exports);
@@ -0,0 +1,23 @@
1
+ import type { CachedItem } from ".";
2
+ export interface Storage {
3
+ /**
4
+ * returns a cached item from the storage layer
5
+ * @param key - key to look up
6
+ */
7
+ getItem(key: string): Promise<CachedItem | undefined>;
8
+ /**
9
+ * sets a cached item on the storage layer
10
+ * @param key - key to store
11
+ * @param content - content to store, including some meta data
12
+ */
13
+ setItem(key: string, content: CachedItem): Promise<void>;
14
+ /**
15
+ * removes item from the storage layer
16
+ * @param key - key to remove
17
+ */
18
+ removeItem(key: string): Promise<void>;
19
+ /**
20
+ * remove all keys from the storage layer
21
+ */
22
+ clear(): Promise<void>;
23
+ }
@@ -1,4 +1,4 @@
1
- import type { CacheContainer, CachingOptions } from "./cache-container";
1
+ import type { CacheContainer, CachingOptions } from "./cacheContainer";
2
2
  declare type WithCacheOptions<Parameters> = Partial<Omit<CachingOptions, "calculateKey">> & {
3
3
  /** an optional prefix to prepend to the key */
4
4
  prefix?: string;
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": "6.0.0",
4
+ "version": "6.0.2",
5
5
  "private": false,
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",
@@ -1,17 +0,0 @@
1
- import type { Storage } from "../storage";
2
- import type { CachingOptions } from "./cache-container-types";
3
- export declare class CacheContainer {
4
- private storage;
5
- constructor(storage: Storage);
6
- getItem<T>(key: string): Promise<{
7
- content: T;
8
- meta: {
9
- expired: boolean;
10
- createdAt: number;
11
- };
12
- } | undefined>;
13
- setItem(key: string, content: unknown, options?: Partial<CachingOptions>): Promise<void>;
14
- clear(): Promise<void>;
15
- private isItemExpired;
16
- unsetKey(key: string): Promise<void>;
17
- }
@@ -1,2 +0,0 @@
1
- export * from "./cache-container";
2
- export * from "./cache-container-types";
@@ -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("./cache-container"), exports);
18
- __exportStar(require("./cache-container-types"), exports);
@@ -1,2 +0,0 @@
1
- import type { CacheContainer, CachingOptions } from "../cache-container";
2
- export declare function Cache(container: CacheContainer, options?: Partial<CachingOptions>): MethodDecorator;
@@ -1 +0,0 @@
1
- export * from "./cache-decorator";
@@ -1,17 +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("./cache-decorator"), exports);
@@ -1 +0,0 @@
1
- export * from "./storage-types";
@@ -1,17 +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("./storage-types"), exports);
@@ -1,7 +0,0 @@
1
- import type { CachedItem } from "../cache-container";
2
- export interface Storage {
3
- getItem(key: string): Promise<CachedItem | undefined>;
4
- setItem(key: string, content: CachedItem): Promise<void>;
5
- removeItem(key: string): Promise<void>;
6
- clear(): Promise<void>;
7
- }
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });