descript-redis-cache 4.0.7 → 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
@@ -8,6 +8,8 @@ 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[];
@@ -87,7 +89,10 @@ export type LoggerEvent = ({
87
89
  export declare class Cache<Result> implements CacheInterface<Result> {
88
90
  #private;
89
91
  constructor(options: Options, logger?: Logger);
90
- getClient(): Cluster | Redis;
92
+ getClient(): {
93
+ reader: Cluster | Redis;
94
+ writer: Cluster | Redis;
95
+ };
91
96
  get({ key }: {
92
97
  key: string;
93
98
  }): Promise<Result | undefined>;
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) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "descript-redis-cache",
3
- "version": "4.0.7",
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",