@sebspark/promise-cache 0.2.8 → 0.2.9

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
@@ -13,7 +13,8 @@ type SetParams<T> = {
13
13
  declare class Persistor {
14
14
  client: ReturnType<typeof createClient> | null;
15
15
  status: 'connected' | 'disconnected';
16
- constructor(isLocalPersistor: boolean);
16
+ redisUrl: string | undefined;
17
+ constructor(redisUrl?: string);
17
18
  connect(onError?: (message: string) => void, onConnect?: (message: string) => void): Promise<void>;
18
19
  size(): Promise<number>;
19
20
  get<T>(key: string): Promise<GetType<T> | null>;
@@ -31,7 +32,11 @@ declare class PromiseCache<U> {
31
32
  * @param ttlInSeconds Default cache TTL.
32
33
  * @param caseSensitive Set to true if you want to differentiate between keys with different casing.
33
34
  */
34
- constructor(ttlInSeconds?: number, caseSensitive?: boolean, isLocalPersistor?: boolean);
35
+ constructor({ ttlInSeconds, caseSensitive, redisUrl, }: {
36
+ ttlInSeconds?: number;
37
+ caseSensitive?: boolean;
38
+ redisUrl?: string;
39
+ });
35
40
  /**
36
41
  * Cache size.
37
42
  * @returns The number of entries in the cache.
package/dist/index.d.ts CHANGED
@@ -13,7 +13,8 @@ type SetParams<T> = {
13
13
  declare class Persistor {
14
14
  client: ReturnType<typeof createClient> | null;
15
15
  status: 'connected' | 'disconnected';
16
- constructor(isLocalPersistor: boolean);
16
+ redisUrl: string | undefined;
17
+ constructor(redisUrl?: string);
17
18
  connect(onError?: (message: string) => void, onConnect?: (message: string) => void): Promise<void>;
18
19
  size(): Promise<number>;
19
20
  get<T>(key: string): Promise<GetType<T> | null>;
@@ -31,7 +32,11 @@ declare class PromiseCache<U> {
31
32
  * @param ttlInSeconds Default cache TTL.
32
33
  * @param caseSensitive Set to true if you want to differentiate between keys with different casing.
33
34
  */
34
- constructor(ttlInSeconds?: number, caseSensitive?: boolean, isLocalPersistor?: boolean);
35
+ constructor({ ttlInSeconds, caseSensitive, redisUrl, }: {
36
+ ttlInSeconds?: number;
37
+ caseSensitive?: boolean;
38
+ redisUrl?: string;
39
+ });
35
40
  /**
36
41
  * Cache size.
37
42
  * @returns The number of entries in the cache.
package/dist/index.js CHANGED
@@ -69,13 +69,6 @@ var createLocalMemoryClient = () => {
69
69
  };
70
70
 
71
71
  // src/persistor.ts
72
- var REDIS_HOST = process.env.REDIS_HOST || "127.0.0.1";
73
- var REDIS_PORT = process.env.REDIS_PORT || 6379;
74
- var REDIS_PASSWORD = process.env.REDIS_PASSWORD || void 0;
75
- var REDIS_URL = `redis://${REDIS_HOST}:${REDIS_PORT}`;
76
- if (REDIS_PASSWORD) {
77
- REDIS_URL = `redis://${REDIS_PASSWORD}@${REDIS_HOST}:${REDIS_PORT}`;
78
- }
79
72
  var CACHE_CLIENT = import_redis.createClient;
80
73
  if (process.env.NODE_ENV === "test") {
81
74
  CACHE_CLIENT = createLocalMemoryClient;
@@ -83,18 +76,21 @@ if (process.env.NODE_ENV === "test") {
83
76
  var Persistor = class {
84
77
  client = null;
85
78
  status = "disconnected";
86
- constructor(isLocalPersistor) {
87
- if (isLocalPersistor) {
79
+ redisUrl;
80
+ constructor(redisUrl) {
81
+ if (redisUrl) {
82
+ this.redisUrl = redisUrl;
83
+ } else {
88
84
  CACHE_CLIENT = createLocalMemoryClient;
89
85
  }
90
86
  this.connect();
91
87
  }
92
88
  async connect(onError, onConnect) {
93
89
  try {
94
- this.client = CACHE_CLIENT({ url: REDIS_URL });
90
+ this.client = CACHE_CLIENT({ url: this.redisUrl });
95
91
  this.client.on("error", (err) => {
96
92
  if (onError) {
97
- onError(`\u274C REDIS | Client Error | ${REDIS_URL} ${err}`);
93
+ onError(`\u274C REDIS | Client Error | ${this.redisUrl} ${err}`);
98
94
  }
99
95
  this.status = "disconnected";
100
96
  });
@@ -106,7 +102,7 @@ var Persistor = class {
106
102
  }
107
103
  this.client.on("connect", () => {
108
104
  if (onConnect) {
109
- onConnect(`\u{1F4E6} REDIS | Connection Ready | ${REDIS_URL}`);
105
+ onConnect(`\u{1F4E6} REDIS | Connection Ready | ${this.redisUrl}`);
110
106
  }
111
107
  this.status = "connected";
112
108
  resolve(true);
@@ -114,7 +110,7 @@ var Persistor = class {
114
110
  });
115
111
  } catch (err) {
116
112
  if (onError) {
117
- onError(`\u274C REDIS | Connection Error | ${REDIS_URL} ${err}`);
113
+ onError(`\u274C REDIS | Connection Error | ${this.redisUrl} ${err}`);
118
114
  }
119
115
  this.status = "disconnected";
120
116
  }
@@ -168,8 +164,8 @@ var Persistor = class {
168
164
  }
169
165
  }
170
166
  };
171
- var createPersistor = (isLocalPersistor) => {
172
- const persistor = new Persistor(isLocalPersistor);
167
+ var createPersistor = (redisUrl) => {
168
+ const persistor = new Persistor(redisUrl);
173
169
  return persistor;
174
170
  };
175
171
 
@@ -184,8 +180,12 @@ var PromiseCache = class {
184
180
  * @param ttlInSeconds Default cache TTL.
185
181
  * @param caseSensitive Set to true if you want to differentiate between keys with different casing.
186
182
  */
187
- constructor(ttlInSeconds, caseSensitive = false, isLocalPersistor = false) {
188
- this.persistor = createPersistor(isLocalPersistor);
183
+ constructor({
184
+ ttlInSeconds,
185
+ caseSensitive = false,
186
+ redisUrl
187
+ }) {
188
+ this.persistor = createPersistor(redisUrl);
189
189
  this.caseSensitive = caseSensitive;
190
190
  if (ttlInSeconds) {
191
191
  this.ttl = ttlInSeconds * 1e3;
package/dist/index.mjs CHANGED
@@ -41,13 +41,6 @@ var createLocalMemoryClient = () => {
41
41
  };
42
42
 
43
43
  // src/persistor.ts
44
- var REDIS_HOST = process.env.REDIS_HOST || "127.0.0.1";
45
- var REDIS_PORT = process.env.REDIS_PORT || 6379;
46
- var REDIS_PASSWORD = process.env.REDIS_PASSWORD || void 0;
47
- var REDIS_URL = `redis://${REDIS_HOST}:${REDIS_PORT}`;
48
- if (REDIS_PASSWORD) {
49
- REDIS_URL = `redis://${REDIS_PASSWORD}@${REDIS_HOST}:${REDIS_PORT}`;
50
- }
51
44
  var CACHE_CLIENT = createClient;
52
45
  if (process.env.NODE_ENV === "test") {
53
46
  CACHE_CLIENT = createLocalMemoryClient;
@@ -55,18 +48,21 @@ if (process.env.NODE_ENV === "test") {
55
48
  var Persistor = class {
56
49
  client = null;
57
50
  status = "disconnected";
58
- constructor(isLocalPersistor) {
59
- if (isLocalPersistor) {
51
+ redisUrl;
52
+ constructor(redisUrl) {
53
+ if (redisUrl) {
54
+ this.redisUrl = redisUrl;
55
+ } else {
60
56
  CACHE_CLIENT = createLocalMemoryClient;
61
57
  }
62
58
  this.connect();
63
59
  }
64
60
  async connect(onError, onConnect) {
65
61
  try {
66
- this.client = CACHE_CLIENT({ url: REDIS_URL });
62
+ this.client = CACHE_CLIENT({ url: this.redisUrl });
67
63
  this.client.on("error", (err) => {
68
64
  if (onError) {
69
- onError(`\u274C REDIS | Client Error | ${REDIS_URL} ${err}`);
65
+ onError(`\u274C REDIS | Client Error | ${this.redisUrl} ${err}`);
70
66
  }
71
67
  this.status = "disconnected";
72
68
  });
@@ -78,7 +74,7 @@ var Persistor = class {
78
74
  }
79
75
  this.client.on("connect", () => {
80
76
  if (onConnect) {
81
- onConnect(`\u{1F4E6} REDIS | Connection Ready | ${REDIS_URL}`);
77
+ onConnect(`\u{1F4E6} REDIS | Connection Ready | ${this.redisUrl}`);
82
78
  }
83
79
  this.status = "connected";
84
80
  resolve(true);
@@ -86,7 +82,7 @@ var Persistor = class {
86
82
  });
87
83
  } catch (err) {
88
84
  if (onError) {
89
- onError(`\u274C REDIS | Connection Error | ${REDIS_URL} ${err}`);
85
+ onError(`\u274C REDIS | Connection Error | ${this.redisUrl} ${err}`);
90
86
  }
91
87
  this.status = "disconnected";
92
88
  }
@@ -140,8 +136,8 @@ var Persistor = class {
140
136
  }
141
137
  }
142
138
  };
143
- var createPersistor = (isLocalPersistor) => {
144
- const persistor = new Persistor(isLocalPersistor);
139
+ var createPersistor = (redisUrl) => {
140
+ const persistor = new Persistor(redisUrl);
145
141
  return persistor;
146
142
  };
147
143
 
@@ -156,8 +152,12 @@ var PromiseCache = class {
156
152
  * @param ttlInSeconds Default cache TTL.
157
153
  * @param caseSensitive Set to true if you want to differentiate between keys with different casing.
158
154
  */
159
- constructor(ttlInSeconds, caseSensitive = false, isLocalPersistor = false) {
160
- this.persistor = createPersistor(isLocalPersistor);
155
+ constructor({
156
+ ttlInSeconds,
157
+ caseSensitive = false,
158
+ redisUrl
159
+ }) {
160
+ this.persistor = createPersistor(redisUrl);
161
161
  this.caseSensitive = caseSensitive;
162
162
  if (ttlInSeconds) {
163
163
  this.ttl = ttlInSeconds * 1e3;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sebspark/promise-cache",
3
- "version": "0.2.8",
3
+ "version": "0.2.9",
4
4
  "license": "Apache-2.0",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",