@sebspark/promise-cache 0.2.8 → 1.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
@@ -13,7 +13,14 @@ 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
+ redis: {
17
+ url: string;
18
+ password?: string;
19
+ } | undefined;
20
+ constructor(redis?: {
21
+ url: string;
22
+ password?: string;
23
+ });
17
24
  connect(onError?: (message: string) => void, onConnect?: (message: string) => void): Promise<void>;
18
25
  size(): Promise<number>;
19
26
  get<T>(key: string): Promise<GetType<T> | null>;
@@ -31,7 +38,14 @@ declare class PromiseCache<U> {
31
38
  * @param ttlInSeconds Default cache TTL.
32
39
  * @param caseSensitive Set to true if you want to differentiate between keys with different casing.
33
40
  */
34
- constructor(ttlInSeconds?: number, caseSensitive?: boolean, isLocalPersistor?: boolean);
41
+ constructor({ ttlInSeconds, caseSensitive, redis, }: {
42
+ ttlInSeconds?: number;
43
+ caseSensitive?: boolean;
44
+ redis?: {
45
+ url: string;
46
+ password?: string;
47
+ };
48
+ });
35
49
  /**
36
50
  * Cache size.
37
51
  * @returns The number of entries in the cache.
package/dist/index.d.ts CHANGED
@@ -13,7 +13,14 @@ 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
+ redis: {
17
+ url: string;
18
+ password?: string;
19
+ } | undefined;
20
+ constructor(redis?: {
21
+ url: string;
22
+ password?: string;
23
+ });
17
24
  connect(onError?: (message: string) => void, onConnect?: (message: string) => void): Promise<void>;
18
25
  size(): Promise<number>;
19
26
  get<T>(key: string): Promise<GetType<T> | null>;
@@ -31,7 +38,14 @@ declare class PromiseCache<U> {
31
38
  * @param ttlInSeconds Default cache TTL.
32
39
  * @param caseSensitive Set to true if you want to differentiate between keys with different casing.
33
40
  */
34
- constructor(ttlInSeconds?: number, caseSensitive?: boolean, isLocalPersistor?: boolean);
41
+ constructor({ ttlInSeconds, caseSensitive, redis, }: {
42
+ ttlInSeconds?: number;
43
+ caseSensitive?: boolean;
44
+ redis?: {
45
+ url: string;
46
+ password?: string;
47
+ };
48
+ });
35
49
  /**
36
50
  * Cache size.
37
51
  * @returns The number of entries in the cache.
package/dist/index.js CHANGED
@@ -69,32 +69,27 @@ 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
- if (process.env.NODE_ENV === "test") {
81
- CACHE_CLIENT = createLocalMemoryClient;
82
- }
83
73
  var Persistor = class {
84
74
  client = null;
85
75
  status = "disconnected";
86
- constructor(isLocalPersistor) {
87
- if (isLocalPersistor) {
76
+ redis;
77
+ constructor(redis) {
78
+ if (redis) {
79
+ this.redis = redis;
80
+ } else {
88
81
  CACHE_CLIENT = createLocalMemoryClient;
89
82
  }
90
83
  this.connect();
91
84
  }
92
85
  async connect(onError, onConnect) {
86
+ var _a;
93
87
  try {
94
- this.client = CACHE_CLIENT({ url: REDIS_URL });
88
+ this.client = CACHE_CLIENT(this.redis);
95
89
  this.client.on("error", (err) => {
90
+ var _a2;
96
91
  if (onError) {
97
- onError(`\u274C REDIS | Client Error | ${REDIS_URL} ${err}`);
92
+ onError(`\u274C REDIS | Client Error | ${(_a2 = this.redis) == null ? void 0 : _a2.url} ${err}`);
98
93
  }
99
94
  this.status = "disconnected";
100
95
  });
@@ -105,8 +100,9 @@ var Persistor = class {
105
100
  return;
106
101
  }
107
102
  this.client.on("connect", () => {
103
+ var _a2;
108
104
  if (onConnect) {
109
- onConnect(`\u{1F4E6} REDIS | Connection Ready | ${REDIS_URL}`);
105
+ onConnect(`\u{1F4E6} REDIS | Connection Ready | ${(_a2 = this.redis) == null ? void 0 : _a2.url}`);
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 | ${(_a = this.redis) == null ? void 0 : _a.url} ${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 = (redis) => {
168
+ const persistor = new Persistor(redis);
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
+ redis
187
+ }) {
188
+ this.persistor = createPersistor(redis);
189
189
  this.caseSensitive = caseSensitive;
190
190
  if (ttlInSeconds) {
191
191
  this.ttl = ttlInSeconds * 1e3;
package/dist/index.mjs CHANGED
@@ -41,32 +41,27 @@ 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
- if (process.env.NODE_ENV === "test") {
53
- CACHE_CLIENT = createLocalMemoryClient;
54
- }
55
45
  var Persistor = class {
56
46
  client = null;
57
47
  status = "disconnected";
58
- constructor(isLocalPersistor) {
59
- if (isLocalPersistor) {
48
+ redis;
49
+ constructor(redis) {
50
+ if (redis) {
51
+ this.redis = redis;
52
+ } else {
60
53
  CACHE_CLIENT = createLocalMemoryClient;
61
54
  }
62
55
  this.connect();
63
56
  }
64
57
  async connect(onError, onConnect) {
58
+ var _a;
65
59
  try {
66
- this.client = CACHE_CLIENT({ url: REDIS_URL });
60
+ this.client = CACHE_CLIENT(this.redis);
67
61
  this.client.on("error", (err) => {
62
+ var _a2;
68
63
  if (onError) {
69
- onError(`\u274C REDIS | Client Error | ${REDIS_URL} ${err}`);
64
+ onError(`\u274C REDIS | Client Error | ${(_a2 = this.redis) == null ? void 0 : _a2.url} ${err}`);
70
65
  }
71
66
  this.status = "disconnected";
72
67
  });
@@ -77,8 +72,9 @@ var Persistor = class {
77
72
  return;
78
73
  }
79
74
  this.client.on("connect", () => {
75
+ var _a2;
80
76
  if (onConnect) {
81
- onConnect(`\u{1F4E6} REDIS | Connection Ready | ${REDIS_URL}`);
77
+ onConnect(`\u{1F4E6} REDIS | Connection Ready | ${(_a2 = this.redis) == null ? void 0 : _a2.url}`);
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 | ${(_a = this.redis) == null ? void 0 : _a.url} ${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 = (redis) => {
140
+ const persistor = new Persistor(redis);
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
+ redis
159
+ }) {
160
+ this.persistor = createPersistor(redis);
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": "1.0.0",
4
4
  "license": "Apache-2.0",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",