@sebspark/promise-cache 1.3.1 → 2.0.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/dist/index.d.mts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { createClient, RedisClientOptions } from 'redis';
2
2
  export { RedisClientOptions } from 'redis';
3
+ import { UUID } from 'node:crypto';
3
4
 
4
5
  type GetType<T> = {
5
6
  value: T;
@@ -13,16 +14,21 @@ type SetParams<T> = {
13
14
  };
14
15
  type PersistorConstructorType = {
15
16
  redis?: RedisClientOptions;
16
- onError?: () => void;
17
- onSuccess?: () => void;
17
+ onError?: (c: string) => void;
18
+ onSuccess?: (c: string) => void;
18
19
  };
19
20
  declare class Persistor {
20
21
  client: ReturnType<typeof createClient> | null;
22
+ private clientId;
23
+ private onError;
24
+ private onSuccess;
21
25
  private readonly redis?;
22
26
  constructor(options: PersistorConstructorType);
23
- connect(onError?: (message: string) => void, onSuccess?: (message: string) => void): Promise<void>;
27
+ connect(): Promise<void>;
28
+ startConnection(): Promise<unknown>;
24
29
  size(): Promise<number>;
25
30
  get<T>(key: string): Promise<GetType<T> | null>;
31
+ getClientId(): UUID;
26
32
  private createOptions;
27
33
  set<T>(key: string, { value, timestamp, ttl }: SetParams<T>): Promise<void>;
28
34
  delete(key: string): Promise<void>;
@@ -32,7 +38,6 @@ declare const createPersistor: ({ redis, onError, onSuccess, }: {
32
38
  onError?: () => void;
33
39
  onSuccess?: () => void;
34
40
  }) => Persistor;
35
- declare const clean: () => void;
36
41
 
37
42
  type PromiseCacheOptions = {
38
43
  ttlInSeconds?: number;
@@ -41,6 +46,7 @@ type PromiseCacheOptions = {
41
46
  onError?: () => void;
42
47
  onSuccess?: () => void;
43
48
  };
49
+ declare const promises: {};
44
50
  declare class PromiseCache<U> {
45
51
  persistor: Persistor;
46
52
  private readonly caseSensitive;
@@ -92,4 +98,4 @@ declare class LocalStorage {
92
98
  }
93
99
  declare const createLocalMemoryClient: () => LocalStorage;
94
100
 
95
- export { LocalStorage, Persistor, PromiseCache, type PromiseCacheOptions, clean, createLocalMemoryClient, createPersistor };
101
+ export { LocalStorage, Persistor, PromiseCache, type PromiseCacheOptions, createLocalMemoryClient, createPersistor, promises };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { createClient, RedisClientOptions } from 'redis';
2
2
  export { RedisClientOptions } from 'redis';
3
+ import { UUID } from 'node:crypto';
3
4
 
4
5
  type GetType<T> = {
5
6
  value: T;
@@ -13,16 +14,21 @@ type SetParams<T> = {
13
14
  };
14
15
  type PersistorConstructorType = {
15
16
  redis?: RedisClientOptions;
16
- onError?: () => void;
17
- onSuccess?: () => void;
17
+ onError?: (c: string) => void;
18
+ onSuccess?: (c: string) => void;
18
19
  };
19
20
  declare class Persistor {
20
21
  client: ReturnType<typeof createClient> | null;
22
+ private clientId;
23
+ private onError;
24
+ private onSuccess;
21
25
  private readonly redis?;
22
26
  constructor(options: PersistorConstructorType);
23
- connect(onError?: (message: string) => void, onSuccess?: (message: string) => void): Promise<void>;
27
+ connect(): Promise<void>;
28
+ startConnection(): Promise<unknown>;
24
29
  size(): Promise<number>;
25
30
  get<T>(key: string): Promise<GetType<T> | null>;
31
+ getClientId(): UUID;
26
32
  private createOptions;
27
33
  set<T>(key: string, { value, timestamp, ttl }: SetParams<T>): Promise<void>;
28
34
  delete(key: string): Promise<void>;
@@ -32,7 +38,6 @@ declare const createPersistor: ({ redis, onError, onSuccess, }: {
32
38
  onError?: () => void;
33
39
  onSuccess?: () => void;
34
40
  }) => Persistor;
35
- declare const clean: () => void;
36
41
 
37
42
  type PromiseCacheOptions = {
38
43
  ttlInSeconds?: number;
@@ -41,6 +46,7 @@ type PromiseCacheOptions = {
41
46
  onError?: () => void;
42
47
  onSuccess?: () => void;
43
48
  };
49
+ declare const promises: {};
44
50
  declare class PromiseCache<U> {
45
51
  persistor: Persistor;
46
52
  private readonly caseSensitive;
@@ -92,4 +98,4 @@ declare class LocalStorage {
92
98
  }
93
99
  declare const createLocalMemoryClient: () => LocalStorage;
94
100
 
95
- export { LocalStorage, Persistor, PromiseCache, type PromiseCacheOptions, clean, createLocalMemoryClient, createPersistor };
101
+ export { LocalStorage, Persistor, PromiseCache, type PromiseCacheOptions, createLocalMemoryClient, createPersistor, promises };
package/dist/index.js CHANGED
@@ -23,13 +23,15 @@ __export(src_exports, {
23
23
  LocalStorage: () => LocalStorage,
24
24
  Persistor: () => Persistor,
25
25
  PromiseCache: () => PromiseCache,
26
- clean: () => clean,
27
26
  createLocalMemoryClient: () => createLocalMemoryClient,
28
- createPersistor: () => createPersistor
27
+ createPersistor: () => createPersistor,
28
+ promises: () => promises
29
29
  });
30
30
  module.exports = __toCommonJS(src_exports);
31
31
 
32
32
  // src/persistor.ts
33
+ var import_node_crypto = require("crypto");
34
+ var import_retry = require("@sebspark/retry");
33
35
  var import_redis = require("redis");
34
36
 
35
37
  // src/localMemory.ts
@@ -76,48 +78,58 @@ var CACHE_CLIENT = import_redis.createClient;
76
78
  var isTestRunning = process.env.NODE_ENV === "test";
77
79
  var Persistor = class {
78
80
  client = null;
81
+ clientId = (0, import_node_crypto.randomUUID)();
82
+ onError;
83
+ onSuccess;
79
84
  redis;
80
85
  constructor(options) {
81
86
  const { redis, onError, onSuccess } = options;
87
+ this.onError = onError;
88
+ this.onSuccess = onSuccess;
82
89
  if (redis && !isTestRunning) {
83
90
  this.redis = redis;
84
91
  } else {
85
92
  CACHE_CLIENT = createLocalMemoryClient;
86
93
  }
87
- this.connect(onError, onSuccess);
94
+ this.connect();
88
95
  }
89
- async connect(onError, onSuccess) {
96
+ async connect() {
97
+ const settings = {
98
+ interval: (x) => {
99
+ return x * 2 * 1e3;
100
+ },
101
+ maxRetries: 5,
102
+ retryCondition: () => {
103
+ console.log("Trying to connect!");
104
+ return true;
105
+ }
106
+ };
107
+ await (0, import_retry.retry)(() => this.startConnection(), settings);
108
+ }
109
+ async startConnection() {
90
110
  var _a, _b;
91
111
  try {
92
112
  this.client = CACHE_CLIENT(this.redis);
93
113
  this.client.on("error", (err) => {
94
114
  var _a2, _b2;
95
- if (onError) {
96
- onError(`\u274C REDIS | Client Error | ${(_a2 = this.redis) == null ? void 0 : _a2.url} ${err}`);
97
- } else {
98
- console.error(`\u274C REDIS | Client Error | ${(_b2 = this.redis) == null ? void 0 : _b2.url} ${err}`);
115
+ if (this.onError) {
116
+ this.onError(`\u274C REDIS | Client Error | ${(_a2 = this.redis) == null ? void 0 : _a2.url} ${err}`);
99
117
  }
118
+ throw new Error(`\u274C REDIS | Client Error | ${(_b2 = this.redis) == null ? void 0 : _b2.url} ${err}`);
100
119
  });
101
- this.client.connect();
102
- await new Promise((resolve, reject) => {
103
- if (!this.client) {
104
- reject("Client not initialized");
105
- return;
120
+ this.client.on("connect", () => {
121
+ var _a2, _b2;
122
+ if (this.onSuccess) {
123
+ this.onSuccess(`\u{1F4E6} REDIS | Connection Ready | ${(_a2 = this.redis) == null ? void 0 : _a2.url}`);
106
124
  }
107
- this.client.on("connect", () => {
108
- var _a2;
109
- if (onSuccess) {
110
- onSuccess(`\u{1F4E6} REDIS | Connection Ready | ${(_a2 = this.redis) == null ? void 0 : _a2.url}`);
111
- }
112
- resolve(true);
113
- });
125
+ console.log(`\u{1F4E6} REDIS | Connection Ready | ${(_b2 = this.redis) == null ? void 0 : _b2.url}`);
114
126
  });
127
+ return await this.client.connect();
115
128
  } catch (err) {
116
- if (onError) {
117
- onError(`\u274C REDIS | Connection Error | ${(_a = this.redis) == null ? void 0 : _a.url} ${err}`);
118
- } else {
119
- console.error(`\u274C REDIS | Connection Error | ${(_b = this.redis) == null ? void 0 : _b.url} ${err}`);
129
+ if (this.onError) {
130
+ this.onError(`\u274C REDIS | Connection Error | ${(_a = this.redis) == null ? void 0 : _a.url} ${err}`);
120
131
  }
132
+ throw new Error(`\u274C REDIS | Connection Error | ${(_b = this.redis) == null ? void 0 : _b.url} ${err}`);
121
133
  }
122
134
  }
123
135
  async size() {
@@ -140,6 +152,9 @@ var Persistor = class {
140
152
  throw new Error(`Error getting data from redis: ${error}`);
141
153
  }
142
154
  }
155
+ getClientId() {
156
+ return this.clientId;
157
+ }
143
158
  createOptions(ttl) {
144
159
  if (ttl !== null && ttl !== void 0) {
145
160
  return { PX: Math.round(ttl) };
@@ -169,33 +184,35 @@ var Persistor = class {
169
184
  }
170
185
  }
171
186
  };
172
- var _persistors = {};
187
+ var persistors = {};
173
188
  var createPersistor = ({
174
189
  redis,
175
190
  onError,
176
191
  onSuccess
177
192
  }) => {
178
193
  if (redis) {
179
- const key = JSON.stringify(redis);
180
- if (!_persistors[key]) {
181
- _persistors[key] = new Persistor({
194
+ let connectionName = redis.url;
195
+ if (redis.name) {
196
+ connectionName = redis.name;
197
+ }
198
+ const key = connectionName;
199
+ if (!persistors[key]) {
200
+ persistors[key] = new Persistor({
182
201
  redis,
183
202
  onError,
184
203
  onSuccess
185
204
  });
186
205
  }
187
- return _persistors[key];
206
+ return persistors[key];
188
207
  }
189
208
  return new Persistor({
190
209
  onSuccess,
191
210
  onError
192
211
  });
193
212
  };
194
- var clean = () => {
195
- _persistors = {};
196
- };
197
213
 
198
214
  // src/promiseCache.ts
215
+ var promises = {};
199
216
  var PromiseCache = class {
200
217
  persistor;
201
218
  caseSensitive;
@@ -283,7 +300,7 @@ var PromiseCache = class {
283
300
  LocalStorage,
284
301
  Persistor,
285
302
  PromiseCache,
286
- clean,
287
303
  createLocalMemoryClient,
288
- createPersistor
304
+ createPersistor,
305
+ promises
289
306
  });
package/dist/index.mjs CHANGED
@@ -1,4 +1,6 @@
1
1
  // src/persistor.ts
2
+ import { randomUUID } from "node:crypto";
3
+ import { retry } from "@sebspark/retry";
2
4
  import { createClient } from "redis";
3
5
 
4
6
  // src/localMemory.ts
@@ -45,48 +47,58 @@ var CACHE_CLIENT = createClient;
45
47
  var isTestRunning = process.env.NODE_ENV === "test";
46
48
  var Persistor = class {
47
49
  client = null;
50
+ clientId = randomUUID();
51
+ onError;
52
+ onSuccess;
48
53
  redis;
49
54
  constructor(options) {
50
55
  const { redis, onError, onSuccess } = options;
56
+ this.onError = onError;
57
+ this.onSuccess = onSuccess;
51
58
  if (redis && !isTestRunning) {
52
59
  this.redis = redis;
53
60
  } else {
54
61
  CACHE_CLIENT = createLocalMemoryClient;
55
62
  }
56
- this.connect(onError, onSuccess);
63
+ this.connect();
57
64
  }
58
- async connect(onError, onSuccess) {
65
+ async connect() {
66
+ const settings = {
67
+ interval: (x) => {
68
+ return x * 2 * 1e3;
69
+ },
70
+ maxRetries: 5,
71
+ retryCondition: () => {
72
+ console.log("Trying to connect!");
73
+ return true;
74
+ }
75
+ };
76
+ await retry(() => this.startConnection(), settings);
77
+ }
78
+ async startConnection() {
59
79
  var _a, _b;
60
80
  try {
61
81
  this.client = CACHE_CLIENT(this.redis);
62
82
  this.client.on("error", (err) => {
63
83
  var _a2, _b2;
64
- if (onError) {
65
- onError(`\u274C REDIS | Client Error | ${(_a2 = this.redis) == null ? void 0 : _a2.url} ${err}`);
66
- } else {
67
- console.error(`\u274C REDIS | Client Error | ${(_b2 = this.redis) == null ? void 0 : _b2.url} ${err}`);
84
+ if (this.onError) {
85
+ this.onError(`\u274C REDIS | Client Error | ${(_a2 = this.redis) == null ? void 0 : _a2.url} ${err}`);
68
86
  }
87
+ throw new Error(`\u274C REDIS | Client Error | ${(_b2 = this.redis) == null ? void 0 : _b2.url} ${err}`);
69
88
  });
70
- this.client.connect();
71
- await new Promise((resolve, reject) => {
72
- if (!this.client) {
73
- reject("Client not initialized");
74
- return;
89
+ this.client.on("connect", () => {
90
+ var _a2, _b2;
91
+ if (this.onSuccess) {
92
+ this.onSuccess(`\u{1F4E6} REDIS | Connection Ready | ${(_a2 = this.redis) == null ? void 0 : _a2.url}`);
75
93
  }
76
- this.client.on("connect", () => {
77
- var _a2;
78
- if (onSuccess) {
79
- onSuccess(`\u{1F4E6} REDIS | Connection Ready | ${(_a2 = this.redis) == null ? void 0 : _a2.url}`);
80
- }
81
- resolve(true);
82
- });
94
+ console.log(`\u{1F4E6} REDIS | Connection Ready | ${(_b2 = this.redis) == null ? void 0 : _b2.url}`);
83
95
  });
96
+ return await this.client.connect();
84
97
  } catch (err) {
85
- if (onError) {
86
- onError(`\u274C REDIS | Connection Error | ${(_a = this.redis) == null ? void 0 : _a.url} ${err}`);
87
- } else {
88
- console.error(`\u274C REDIS | Connection Error | ${(_b = this.redis) == null ? void 0 : _b.url} ${err}`);
98
+ if (this.onError) {
99
+ this.onError(`\u274C REDIS | Connection Error | ${(_a = this.redis) == null ? void 0 : _a.url} ${err}`);
89
100
  }
101
+ throw new Error(`\u274C REDIS | Connection Error | ${(_b = this.redis) == null ? void 0 : _b.url} ${err}`);
90
102
  }
91
103
  }
92
104
  async size() {
@@ -109,6 +121,9 @@ var Persistor = class {
109
121
  throw new Error(`Error getting data from redis: ${error}`);
110
122
  }
111
123
  }
124
+ getClientId() {
125
+ return this.clientId;
126
+ }
112
127
  createOptions(ttl) {
113
128
  if (ttl !== null && ttl !== void 0) {
114
129
  return { PX: Math.round(ttl) };
@@ -138,33 +153,35 @@ var Persistor = class {
138
153
  }
139
154
  }
140
155
  };
141
- var _persistors = {};
156
+ var persistors = {};
142
157
  var createPersistor = ({
143
158
  redis,
144
159
  onError,
145
160
  onSuccess
146
161
  }) => {
147
162
  if (redis) {
148
- const key = JSON.stringify(redis);
149
- if (!_persistors[key]) {
150
- _persistors[key] = new Persistor({
163
+ let connectionName = redis.url;
164
+ if (redis.name) {
165
+ connectionName = redis.name;
166
+ }
167
+ const key = connectionName;
168
+ if (!persistors[key]) {
169
+ persistors[key] = new Persistor({
151
170
  redis,
152
171
  onError,
153
172
  onSuccess
154
173
  });
155
174
  }
156
- return _persistors[key];
175
+ return persistors[key];
157
176
  }
158
177
  return new Persistor({
159
178
  onSuccess,
160
179
  onError
161
180
  });
162
181
  };
163
- var clean = () => {
164
- _persistors = {};
165
- };
166
182
 
167
183
  // src/promiseCache.ts
184
+ var promises = {};
168
185
  var PromiseCache = class {
169
186
  persistor;
170
187
  caseSensitive;
@@ -251,7 +268,7 @@ export {
251
268
  LocalStorage,
252
269
  Persistor,
253
270
  PromiseCache,
254
- clean,
255
271
  createLocalMemoryClient,
256
- createPersistor
272
+ createPersistor,
273
+ promises
257
274
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sebspark/promise-cache",
3
- "version": "1.3.1",
3
+ "version": "2.0.0",
4
4
  "license": "Apache-2.0",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -19,6 +19,7 @@
19
19
  "tsconfig": "*"
20
20
  },
21
21
  "dependencies": {
22
- "redis": "4.6.14"
22
+ "redis": "4.6.14",
23
+ "@sebspark/retry": "*"
23
24
  }
24
25
  }