descript-redis-cache 4.0.1 → 4.0.3

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
@@ -5,15 +5,23 @@ Plugin to use Redis as a cache in Descript
5
5
 
6
6
  ```js
7
7
  import de from 'descript';
8
- import deRedisCache from 'descript-redis-cache';
8
+ import { Cache } from 'descript-redis-cache';
9
9
 
10
- const redisCache = new deRedisCache(options);
10
+ const redisCache = new Cache(options);
11
+
12
+ // subscribe to events if necessary
13
+ // @see https://github.com/luin/ioredis#connection-events
14
+ redisCache.getClient()
15
+ .on('reconnecting', () => {/* ... */})
16
+ .on('error', () => {/* ... */})
17
+ .on('close', () => {/* ... */})
18
+ .on('end', () => {/* ... */});
11
19
 
12
20
  const myBlock = de.http({
13
21
  block: { /* ... */ },
14
22
  options: {
15
- key: ({ params }) => '_some_cache_key_from_params_',
16
- cache: deRedisCache,
23
+ key: ({ params }) => '_some_cache_key_by_params_',
24
+ cache: redisCache,
17
25
  }
18
26
  });
19
27
  ```
@@ -0,0 +1,136 @@
1
+ import type { CacheInterface } from 'descript';
2
+ import type { ClusterNode, ClusterOptions, RedisOptions } from 'ioredis';
3
+ import { Cluster, Redis } from 'ioredis';
4
+ export interface Options {
5
+ defaultKeyTTL?: number;
6
+ generation?: number;
7
+ readTimeout?: number;
8
+ redis: ({
9
+ startupNodes: ClusterNode[];
10
+ options?: ClusterOptions;
11
+ } | {
12
+ options: RedisOptions;
13
+ });
14
+ }
15
+ interface Logger {
16
+ log(event: LoggerEvent): void;
17
+ }
18
+ export type LoggerEvent = ({
19
+ type: EVENT.REDIS_CACHE_INITIALIZED;
20
+ options: Options;
21
+ } | {
22
+ type: EVENT.REDIS_CACHE_READ_START;
23
+ key: string;
24
+ normalizedKey: string;
25
+ } | {
26
+ type: EVENT.REDIS_CACHE_READ_KEY_NOT_FOUND;
27
+ key: string;
28
+ normalizedKey: string;
29
+ timers: {
30
+ network: [number, number];
31
+ total: [number, number];
32
+ };
33
+ } | {
34
+ type: EVENT.REDIS_CACHE_READ_TIMEOUT;
35
+ key: string;
36
+ normalizedKey: string;
37
+ timers: {
38
+ network: [number, number];
39
+ total: [number, number];
40
+ };
41
+ } | {
42
+ type: EVENT.REDIS_CACHE_READ_ERROR;
43
+ error: Error;
44
+ key: string;
45
+ normalizedKey: string;
46
+ timers: {
47
+ network: [number, number];
48
+ total: [number, number];
49
+ };
50
+ } | {
51
+ type: EVENT.REDIS_CACHE_JSON_PARSING_FAILED;
52
+ data: unknown;
53
+ error: unknown;
54
+ key: string;
55
+ normalizedKey: string;
56
+ timers: {
57
+ network: [number, number];
58
+ total: [number, number];
59
+ };
60
+ } | {
61
+ type: EVENT.REDIS_CACHE_READ_DONE;
62
+ data: unknown;
63
+ key: string;
64
+ normalizedKey: string;
65
+ timers: {
66
+ network: [number, number];
67
+ total: [number, number];
68
+ };
69
+ } | {
70
+ type: EVENT.REDIS_CACHE_WRITE_START;
71
+ key: string;
72
+ normalizedKey: string;
73
+ } | {
74
+ type: EVENT.REDIS_CACHE_JSON_STRINGIFY_FAILED;
75
+ data: unknown;
76
+ error: unknown;
77
+ key: string;
78
+ normalizedKey: string;
79
+ timers: {
80
+ total: [number, number];
81
+ };
82
+ } | {
83
+ type: EVENT.REDIS_CACHE_WRITE_ERROR;
84
+ error: unknown;
85
+ key: string;
86
+ normalizedKey: string;
87
+ timers: {
88
+ network: [number, number];
89
+ total: [number, number];
90
+ };
91
+ } | {
92
+ type: EVENT.REDIS_CACHE_WRITE_FAILED;
93
+ key: string;
94
+ normalizedKey: string;
95
+ timers: {
96
+ network: [number, number];
97
+ total: [number, number];
98
+ };
99
+ } | {
100
+ type: EVENT.REDIS_CACHE_WRITE_DONE;
101
+ data: string;
102
+ key: string;
103
+ normalizedKey: string;
104
+ timers: {
105
+ network: [number, number];
106
+ total: [number, number];
107
+ };
108
+ });
109
+ export declare class Cache<Result> implements CacheInterface<Result> {
110
+ #private;
111
+ constructor(options: Options, logger?: Logger);
112
+ getClient(): Cluster | Redis;
113
+ get({ key }: {
114
+ key: string;
115
+ }): Promise<Result | undefined>;
116
+ set({ key, value, maxage }: {
117
+ key: string;
118
+ value: unknown;
119
+ maxage?: number;
120
+ }): Promise<void>;
121
+ }
122
+ export declare const enum EVENT {
123
+ REDIS_CACHE_INITIALIZED = "REDIS_CACHE_INITIALIZED",
124
+ REDIS_CACHE_JSON_PARSING_FAILED = "REDIS_CACHE_JSON_PARSING_FAILED",
125
+ REDIS_CACHE_JSON_STRINGIFY_FAILED = "REDIS_CACHE_JSON_STRINGIFY_FAILED",
126
+ REDIS_CACHE_READ_DONE = "REDIS_CACHE_READ_DONE",
127
+ REDIS_CACHE_READ_ERROR = "REDIS_CACHE_READ_ERROR",
128
+ REDIS_CACHE_READ_KEY_NOT_FOUND = "REDIS_CACHE_READ_KEY_NOT_FOUND",
129
+ REDIS_CACHE_READ_START = "REDIS_CACHE_READ_START",
130
+ REDIS_CACHE_READ_TIMEOUT = "REDIS_CACHE_READ_TIMEOUT",
131
+ REDIS_CACHE_WRITE_DONE = "REDIS_CACHE_WRITE_DONE",
132
+ REDIS_CACHE_WRITE_ERROR = "REDIS_CACHE_WRITE_ERROR",
133
+ REDIS_CACHE_WRITE_FAILED = "REDIS_CACHE_WRITE_FAILED",
134
+ REDIS_CACHE_WRITE_START = "REDIS_CACHE_WRITE_START"
135
+ }
136
+ export {};
package/build/index.js CHANGED
@@ -43,6 +43,9 @@ class Cache {
43
43
  options: { ...__classPrivateFieldGet(this, _Cache_options, "f") },
44
44
  });
45
45
  }
46
+ getClient() {
47
+ return __classPrivateFieldGet(this, _Cache_client, "f");
48
+ }
46
49
  get({ key }) {
47
50
  const normalizedKey = __classPrivateFieldGet(this, _Cache_instances, "m", _Cache_normalizeKey).call(this, key);
48
51
  return new Promise((resolve, reject) => {
package/package.json CHANGED
@@ -1,8 +1,9 @@
1
1
  {
2
2
  "name": "descript-redis-cache",
3
- "version": "4.0.1",
3
+ "version": "4.0.3",
4
4
  "description": "plugin for descript to use redis as cache",
5
5
  "main": "./build/index.js",
6
+ "types": "./build/index.d.ts",
6
7
  "scripts": {
7
8
  "eslint": "npx eslint .",
8
9
  "test": "npx vitest",