@sebspark/promise-cache 0.2.7 → 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,9 +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_URL = `redis://${REDIS_HOST}:${REDIS_PORT}`;
75
72
  var CACHE_CLIENT = import_redis.createClient;
76
73
  if (process.env.NODE_ENV === "test") {
77
74
  CACHE_CLIENT = createLocalMemoryClient;
@@ -79,18 +76,21 @@ if (process.env.NODE_ENV === "test") {
79
76
  var Persistor = class {
80
77
  client = null;
81
78
  status = "disconnected";
82
- constructor(isLocalPersistor) {
83
- if (isLocalPersistor) {
79
+ redisUrl;
80
+ constructor(redisUrl) {
81
+ if (redisUrl) {
82
+ this.redisUrl = redisUrl;
83
+ } else {
84
84
  CACHE_CLIENT = createLocalMemoryClient;
85
85
  }
86
86
  this.connect();
87
87
  }
88
88
  async connect(onError, onConnect) {
89
89
  try {
90
- this.client = CACHE_CLIENT({ url: REDIS_URL });
90
+ this.client = CACHE_CLIENT({ url: this.redisUrl });
91
91
  this.client.on("error", (err) => {
92
92
  if (onError) {
93
- onError(`\u274C REDIS | Client Error | ${REDIS_URL} ${err}`);
93
+ onError(`\u274C REDIS | Client Error | ${this.redisUrl} ${err}`);
94
94
  }
95
95
  this.status = "disconnected";
96
96
  });
@@ -102,7 +102,7 @@ var Persistor = class {
102
102
  }
103
103
  this.client.on("connect", () => {
104
104
  if (onConnect) {
105
- onConnect(`\u{1F4E6} REDIS | Connection Ready | ${REDIS_URL}`);
105
+ onConnect(`\u{1F4E6} REDIS | Connection Ready | ${this.redisUrl}`);
106
106
  }
107
107
  this.status = "connected";
108
108
  resolve(true);
@@ -110,7 +110,7 @@ var Persistor = class {
110
110
  });
111
111
  } catch (err) {
112
112
  if (onError) {
113
- onError(`\u274C REDIS | Connection Error | ${REDIS_URL} ${err}`);
113
+ onError(`\u274C REDIS | Connection Error | ${this.redisUrl} ${err}`);
114
114
  }
115
115
  this.status = "disconnected";
116
116
  }
@@ -164,8 +164,8 @@ var Persistor = class {
164
164
  }
165
165
  }
166
166
  };
167
- var createPersistor = (isLocalPersistor) => {
168
- const persistor = new Persistor(isLocalPersistor);
167
+ var createPersistor = (redisUrl) => {
168
+ const persistor = new Persistor(redisUrl);
169
169
  return persistor;
170
170
  };
171
171
 
@@ -180,8 +180,12 @@ var PromiseCache = class {
180
180
  * @param ttlInSeconds Default cache TTL.
181
181
  * @param caseSensitive Set to true if you want to differentiate between keys with different casing.
182
182
  */
183
- constructor(ttlInSeconds, caseSensitive = false, isLocalPersistor = false) {
184
- this.persistor = createPersistor(isLocalPersistor);
183
+ constructor({
184
+ ttlInSeconds,
185
+ caseSensitive = false,
186
+ redisUrl
187
+ }) {
188
+ this.persistor = createPersistor(redisUrl);
185
189
  this.caseSensitive = caseSensitive;
186
190
  if (ttlInSeconds) {
187
191
  this.ttl = ttlInSeconds * 1e3;
package/dist/index.mjs CHANGED
@@ -41,9 +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_URL = `redis://${REDIS_HOST}:${REDIS_PORT}`;
47
44
  var CACHE_CLIENT = createClient;
48
45
  if (process.env.NODE_ENV === "test") {
49
46
  CACHE_CLIENT = createLocalMemoryClient;
@@ -51,18 +48,21 @@ if (process.env.NODE_ENV === "test") {
51
48
  var Persistor = class {
52
49
  client = null;
53
50
  status = "disconnected";
54
- constructor(isLocalPersistor) {
55
- if (isLocalPersistor) {
51
+ redisUrl;
52
+ constructor(redisUrl) {
53
+ if (redisUrl) {
54
+ this.redisUrl = redisUrl;
55
+ } else {
56
56
  CACHE_CLIENT = createLocalMemoryClient;
57
57
  }
58
58
  this.connect();
59
59
  }
60
60
  async connect(onError, onConnect) {
61
61
  try {
62
- this.client = CACHE_CLIENT({ url: REDIS_URL });
62
+ this.client = CACHE_CLIENT({ url: this.redisUrl });
63
63
  this.client.on("error", (err) => {
64
64
  if (onError) {
65
- onError(`\u274C REDIS | Client Error | ${REDIS_URL} ${err}`);
65
+ onError(`\u274C REDIS | Client Error | ${this.redisUrl} ${err}`);
66
66
  }
67
67
  this.status = "disconnected";
68
68
  });
@@ -74,7 +74,7 @@ var Persistor = class {
74
74
  }
75
75
  this.client.on("connect", () => {
76
76
  if (onConnect) {
77
- onConnect(`\u{1F4E6} REDIS | Connection Ready | ${REDIS_URL}`);
77
+ onConnect(`\u{1F4E6} REDIS | Connection Ready | ${this.redisUrl}`);
78
78
  }
79
79
  this.status = "connected";
80
80
  resolve(true);
@@ -82,7 +82,7 @@ var Persistor = class {
82
82
  });
83
83
  } catch (err) {
84
84
  if (onError) {
85
- onError(`\u274C REDIS | Connection Error | ${REDIS_URL} ${err}`);
85
+ onError(`\u274C REDIS | Connection Error | ${this.redisUrl} ${err}`);
86
86
  }
87
87
  this.status = "disconnected";
88
88
  }
@@ -136,8 +136,8 @@ var Persistor = class {
136
136
  }
137
137
  }
138
138
  };
139
- var createPersistor = (isLocalPersistor) => {
140
- const persistor = new Persistor(isLocalPersistor);
139
+ var createPersistor = (redisUrl) => {
140
+ const persistor = new Persistor(redisUrl);
141
141
  return persistor;
142
142
  };
143
143
 
@@ -152,8 +152,12 @@ var PromiseCache = class {
152
152
  * @param ttlInSeconds Default cache TTL.
153
153
  * @param caseSensitive Set to true if you want to differentiate between keys with different casing.
154
154
  */
155
- constructor(ttlInSeconds, caseSensitive = false, isLocalPersistor = false) {
156
- this.persistor = createPersistor(isLocalPersistor);
155
+ constructor({
156
+ ttlInSeconds,
157
+ caseSensitive = false,
158
+ redisUrl
159
+ }) {
160
+ this.persistor = createPersistor(redisUrl);
157
161
  this.caseSensitive = caseSensitive;
158
162
  if (ttlInSeconds) {
159
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.7",
3
+ "version": "0.2.9",
4
4
  "license": "Apache-2.0",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",