descript-redis-cache 4.0.6 → 4.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/build/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { CacheInterface } from 'descript';
1
+ import type { CacheInterface, LoggerInterface } from 'descript';
2
2
  import type { ClusterNode, ClusterOptions, RedisOptions } from 'ioredis';
3
3
  import { Cluster, Redis } from 'ioredis';
4
4
  export interface Options {
@@ -8,15 +8,15 @@ export interface Options {
8
8
  generation?: number;
9
9
  /** read timeout in milliseconds (default: 100) */
10
10
  readTimeout?: number;
11
+ /** use two clients (reader and writer) with Sentinel (default: false) */
12
+ useReaderAndWriterWithSentinel?: boolean;
11
13
  /** redis config */
12
14
  redis: RedisOptions | {
13
15
  startupNodes: ClusterNode[];
14
16
  options?: ClusterOptions;
15
17
  };
16
18
  }
17
- interface Logger {
18
- log(event: LoggerEvent): void;
19
- }
19
+ type Logger = LoggerInterface<LoggerEvent>;
20
20
  interface Timers {
21
21
  start: number;
22
22
  end: number;
@@ -24,9 +24,6 @@ interface Timers {
24
24
  export type LoggerEvent = ({
25
25
  type: EVENT.REDIS_CACHE_INITIALIZED;
26
26
  options: Options;
27
- } | {
28
- type: EVENT.REDIS_CACHE_ERROR;
29
- error: Error;
30
27
  } | {
31
28
  type: EVENT.REDIS_CACHE_READ_START;
32
29
  key: string;
@@ -73,7 +70,7 @@ export type LoggerEvent = ({
73
70
  timers: Timers;
74
71
  } | {
75
72
  type: EVENT.REDIS_CACHE_WRITE_ERROR;
76
- error: unknown;
73
+ error: Error;
77
74
  key: string;
78
75
  normalizedKey: string;
79
76
  timers: Timers;
@@ -92,7 +89,10 @@ export type LoggerEvent = ({
92
89
  export declare class Cache<Result> implements CacheInterface<Result> {
93
90
  #private;
94
91
  constructor(options: Options, logger?: Logger);
95
- getClient(): Cluster | Redis;
92
+ getClient(): {
93
+ reader: Cluster | Redis;
94
+ writer: Cluster | Redis;
95
+ };
96
96
  get({ key }: {
97
97
  key: string;
98
98
  }): Promise<Result | undefined>;
@@ -104,7 +104,6 @@ export declare class Cache<Result> implements CacheInterface<Result> {
104
104
  }
105
105
  export declare enum EVENT {
106
106
  REDIS_CACHE_INITIALIZED = "REDIS_CACHE_INITIALIZED",
107
- REDIS_CACHE_ERROR = "REDIS_CACHE_ERROR",
108
107
  REDIS_CACHE_JSON_PARSING_FAILED = "REDIS_CACHE_JSON_PARSING_FAILED",
109
108
  REDIS_CACHE_JSON_STRINGIFY_FAILED = "REDIS_CACHE_JSON_STRINGIFY_FAILED",
110
109
  REDIS_CACHE_READ_DONE = "REDIS_CACHE_READ_DONE",
package/build/index.js CHANGED
@@ -10,7 +10,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
10
10
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
11
11
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
12
  };
13
- var _Cache_instances, _Cache_client, _Cache_logger, _Cache_options, _Cache_normalizeKey, _Cache_log;
13
+ var _Cache_instances, _Cache_writer, _Cache_reader, _Cache_logger, _Cache_options, _Cache_normalizeKey, _Cache_log;
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.EVENT = exports.Cache = void 0;
16
16
  const node_crypto_1 = require("node:crypto");
@@ -19,7 +19,8 @@ const ioredis_1 = require("ioredis");
19
19
  class Cache {
20
20
  constructor(options, logger) {
21
21
  _Cache_instances.add(this);
22
- _Cache_client.set(this, void 0);
22
+ _Cache_writer.set(this, void 0);
23
+ _Cache_reader.set(this, void 0);
23
24
  _Cache_logger.set(this, void 0);
24
25
  _Cache_options.set(this, void 0);
25
26
  __classPrivateFieldSet(this, _Cache_options, {
@@ -30,10 +31,27 @@ class Cache {
30
31
  }, "f");
31
32
  __classPrivateFieldSet(this, _Cache_logger, logger, "f");
32
33
  if ('startupNodes' in __classPrivateFieldGet(this, _Cache_options, "f").redis) {
33
- __classPrivateFieldSet(this, _Cache_client, new ioredis_1.Cluster(__classPrivateFieldGet(this, _Cache_options, "f").redis.startupNodes, __classPrivateFieldGet(this, _Cache_options, "f").redis.options), "f");
34
+ __classPrivateFieldSet(this, _Cache_reader, new ioredis_1.Cluster(__classPrivateFieldGet(this, _Cache_options, "f").redis.startupNodes, __classPrivateFieldGet(this, _Cache_options, "f").redis.options), "f");
35
+ __classPrivateFieldSet(this, _Cache_writer, __classPrivateFieldGet(this, _Cache_reader, "f"), "f");
34
36
  }
35
37
  else {
36
- __classPrivateFieldSet(this, _Cache_client, new ioredis_1.Redis(__classPrivateFieldGet(this, _Cache_options, "f").redis), "f");
38
+ if (__classPrivateFieldGet(this, _Cache_options, "f").useReaderAndWriterWithSentinel) {
39
+ // Client for write (always on master)
40
+ __classPrivateFieldSet(this, _Cache_writer, new ioredis_1.Redis({
41
+ ...__classPrivateFieldGet(this, _Cache_options, "f").redis,
42
+ role: 'master',
43
+ }), "f");
44
+ // Client for read (replica, only read-only commands)
45
+ __classPrivateFieldSet(this, _Cache_reader, new ioredis_1.Redis({
46
+ ...__classPrivateFieldGet(this, _Cache_options, "f").redis,
47
+ role: 'slave',
48
+ readOnly: true,
49
+ }), "f");
50
+ }
51
+ else {
52
+ __classPrivateFieldSet(this, _Cache_reader, new ioredis_1.Redis(__classPrivateFieldGet(this, _Cache_options, "f").redis), "f");
53
+ __classPrivateFieldSet(this, _Cache_writer, __classPrivateFieldGet(this, _Cache_reader, "f"), "f");
54
+ }
37
55
  }
38
56
  __classPrivateFieldGet(this, _Cache_instances, "m", _Cache_log).call(this, {
39
57
  type: EVENT.REDIS_CACHE_INITIALIZED,
@@ -41,7 +59,10 @@ class Cache {
41
59
  });
42
60
  }
43
61
  getClient() {
44
- return __classPrivateFieldGet(this, _Cache_client, "f");
62
+ return {
63
+ reader: __classPrivateFieldGet(this, _Cache_reader, "f"),
64
+ writer: __classPrivateFieldGet(this, _Cache_writer, "f"),
65
+ };
45
66
  }
46
67
  get({ key }) {
47
68
  const normalizedKey = __classPrivateFieldGet(this, _Cache_instances, "m", _Cache_normalizeKey).call(this, key);
@@ -68,7 +89,7 @@ class Cache {
68
89
  id: EVENT.REDIS_CACHE_READ_TIMEOUT,
69
90
  }));
70
91
  }, __classPrivateFieldGet(this, _Cache_options, "f").readTimeout);
71
- __classPrivateFieldGet(this, _Cache_client, "f").get(normalizedKey, (error, data) => {
92
+ __classPrivateFieldGet(this, _Cache_reader, "f").get(normalizedKey, (error, data) => {
72
93
  if (isTimeout) {
73
94
  return;
74
95
  }
@@ -173,7 +194,7 @@ class Cache {
173
194
  return;
174
195
  }
175
196
  // maxage - seconds
176
- __classPrivateFieldGet(this, _Cache_client, "f").set(normalizedKey, json, 'EX', maxage, (error, done) => {
197
+ __classPrivateFieldGet(this, _Cache_writer, "f").set(normalizedKey, json, 'EX', maxage, (error, done) => {
177
198
  if (error) {
178
199
  __classPrivateFieldGet(this, _Cache_instances, "m", _Cache_log).call(this, {
179
200
  type: EVENT.REDIS_CACHE_WRITE_ERROR,
@@ -221,7 +242,7 @@ class Cache {
221
242
  }
222
243
  }
223
244
  exports.Cache = Cache;
224
- _Cache_client = new WeakMap(), _Cache_logger = new WeakMap(), _Cache_options = new WeakMap(), _Cache_instances = new WeakSet(), _Cache_normalizeKey = function _Cache_normalizeKey(key) {
245
+ _Cache_writer = new WeakMap(), _Cache_reader = new WeakMap(), _Cache_logger = new WeakMap(), _Cache_options = new WeakMap(), _Cache_instances = new WeakSet(), _Cache_normalizeKey = function _Cache_normalizeKey(key) {
225
246
  const value = `g${__classPrivateFieldGet(this, _Cache_options, "f").generation}:${key}`;
226
247
  return (0, node_crypto_1.hash)('sha512', value);
227
248
  }, _Cache_log = function _Cache_log(event) {
@@ -232,7 +253,6 @@ _Cache_client = new WeakMap(), _Cache_logger = new WeakMap(), _Cache_options = n
232
253
  var EVENT;
233
254
  (function (EVENT) {
234
255
  EVENT["REDIS_CACHE_INITIALIZED"] = "REDIS_CACHE_INITIALIZED";
235
- EVENT["REDIS_CACHE_ERROR"] = "REDIS_CACHE_ERROR";
236
256
  EVENT["REDIS_CACHE_JSON_PARSING_FAILED"] = "REDIS_CACHE_JSON_PARSING_FAILED";
237
257
  EVENT["REDIS_CACHE_JSON_STRINGIFY_FAILED"] = "REDIS_CACHE_JSON_STRINGIFY_FAILED";
238
258
  EVENT["REDIS_CACHE_READ_DONE"] = "REDIS_CACHE_READ_DONE";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "descript-redis-cache",
3
- "version": "4.0.6",
3
+ "version": "4.1.0",
4
4
  "description": "plugin for descript to use redis as cache",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",
@@ -41,7 +41,7 @@
41
41
  "@stylistic/eslint-plugin-ts": "^2.11.0",
42
42
  "@types/eslint__js": "^8.42.3",
43
43
  "@types/node": "^22.10.0",
44
- "descript": "^4.0.5",
44
+ "descript": "^4.0.6",
45
45
  "eslint": "^9.15.0",
46
46
  "typescript": "^5.7.2",
47
47
  "typescript-eslint": "^8.16.0",