@sebspark/promise-cache 2.0.9 → 2.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/dist/index.d.mts +2 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +10 -4
- package/dist/index.mjs +10 -4
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -75,9 +75,10 @@ declare class PromiseCache<U> {
|
|
|
75
75
|
* @param key Cache key.
|
|
76
76
|
* @param delegate The function to execute if the key is not in the cache.
|
|
77
77
|
* @param ttlInSeconds Time to live in seconds.
|
|
78
|
+
* @param ttlKey The key in the response object that contains the TTL.
|
|
78
79
|
* @returns The result of the delegate function.
|
|
79
80
|
*/
|
|
80
|
-
wrap(key: string, delegate: () => Promise<U>, ttlInSeconds?: number): Promise<U>;
|
|
81
|
+
wrap(key: string, delegate: () => Promise<U>, ttlInSeconds?: number, ttlKey?: string): Promise<U>;
|
|
81
82
|
}
|
|
82
83
|
|
|
83
84
|
declare class LocalStorage {
|
package/dist/index.d.ts
CHANGED
|
@@ -75,9 +75,10 @@ declare class PromiseCache<U> {
|
|
|
75
75
|
* @param key Cache key.
|
|
76
76
|
* @param delegate The function to execute if the key is not in the cache.
|
|
77
77
|
* @param ttlInSeconds Time to live in seconds.
|
|
78
|
+
* @param ttlKey The key in the response object that contains the TTL.
|
|
78
79
|
* @returns The result of the delegate function.
|
|
79
80
|
*/
|
|
80
|
-
wrap(key: string, delegate: () => Promise<U>, ttlInSeconds?: number): Promise<U>;
|
|
81
|
+
wrap(key: string, delegate: () => Promise<U>, ttlInSeconds?: number, ttlKey?: string): Promise<U>;
|
|
81
82
|
}
|
|
82
83
|
|
|
83
84
|
declare class LocalStorage {
|
package/dist/index.js
CHANGED
|
@@ -104,8 +104,9 @@ var Persistor = class {
|
|
|
104
104
|
async startConnection() {
|
|
105
105
|
try {
|
|
106
106
|
await new Promise((resolve, reject) => {
|
|
107
|
+
var _a;
|
|
107
108
|
this.client = CACHE_CLIENT({
|
|
108
|
-
|
|
109
|
+
url: (_a = this.redis) == null ? void 0 : _a.url,
|
|
109
110
|
socket: {
|
|
110
111
|
reconnectStrategy: (retries, cause) => {
|
|
111
112
|
console.error(cause);
|
|
@@ -282,15 +283,16 @@ var PromiseCache = class {
|
|
|
282
283
|
* @param key Cache key.
|
|
283
284
|
* @param delegate The function to execute if the key is not in the cache.
|
|
284
285
|
* @param ttlInSeconds Time to live in seconds.
|
|
286
|
+
* @param ttlKey The key in the response object that contains the TTL.
|
|
285
287
|
* @returns The result of the delegate function.
|
|
286
288
|
*/
|
|
287
|
-
async wrap(key, delegate, ttlInSeconds) {
|
|
289
|
+
async wrap(key, delegate, ttlInSeconds, ttlKey) {
|
|
288
290
|
const now = Date.now();
|
|
289
291
|
const effectiveKey = this.caseSensitive ? key : key.toLowerCase();
|
|
290
|
-
|
|
292
|
+
let effectiveTTL = ttlInSeconds !== void 0 ? ttlInSeconds * 1e3 : this.ttl;
|
|
291
293
|
const cached = await this.persistor.get(effectiveKey);
|
|
292
294
|
if (cached) {
|
|
293
|
-
if (cached.ttl !== effectiveTTL) {
|
|
295
|
+
if (!ttlKey && cached.ttl !== effectiveTTL) {
|
|
294
296
|
console.error(
|
|
295
297
|
`WARNING: TTL mismatch for key: ${effectiveKey}. It is recommended to use the same TTL for the same key.`
|
|
296
298
|
);
|
|
@@ -298,6 +300,10 @@ var PromiseCache = class {
|
|
|
298
300
|
return cached.value;
|
|
299
301
|
}
|
|
300
302
|
const response = await delegate();
|
|
303
|
+
if (ttlKey) {
|
|
304
|
+
const responseDict = response;
|
|
305
|
+
effectiveTTL = Number(responseDict[ttlKey]) || effectiveTTL;
|
|
306
|
+
}
|
|
301
307
|
this.persistor.set(effectiveKey, {
|
|
302
308
|
value: response,
|
|
303
309
|
timestamp: now,
|
package/dist/index.mjs
CHANGED
|
@@ -75,8 +75,9 @@ var Persistor = class {
|
|
|
75
75
|
async startConnection() {
|
|
76
76
|
try {
|
|
77
77
|
await new Promise((resolve, reject) => {
|
|
78
|
+
var _a;
|
|
78
79
|
this.client = CACHE_CLIENT({
|
|
79
|
-
|
|
80
|
+
url: (_a = this.redis) == null ? void 0 : _a.url,
|
|
80
81
|
socket: {
|
|
81
82
|
reconnectStrategy: (retries, cause) => {
|
|
82
83
|
console.error(cause);
|
|
@@ -253,15 +254,16 @@ var PromiseCache = class {
|
|
|
253
254
|
* @param key Cache key.
|
|
254
255
|
* @param delegate The function to execute if the key is not in the cache.
|
|
255
256
|
* @param ttlInSeconds Time to live in seconds.
|
|
257
|
+
* @param ttlKey The key in the response object that contains the TTL.
|
|
256
258
|
* @returns The result of the delegate function.
|
|
257
259
|
*/
|
|
258
|
-
async wrap(key, delegate, ttlInSeconds) {
|
|
260
|
+
async wrap(key, delegate, ttlInSeconds, ttlKey) {
|
|
259
261
|
const now = Date.now();
|
|
260
262
|
const effectiveKey = this.caseSensitive ? key : key.toLowerCase();
|
|
261
|
-
|
|
263
|
+
let effectiveTTL = ttlInSeconds !== void 0 ? ttlInSeconds * 1e3 : this.ttl;
|
|
262
264
|
const cached = await this.persistor.get(effectiveKey);
|
|
263
265
|
if (cached) {
|
|
264
|
-
if (cached.ttl !== effectiveTTL) {
|
|
266
|
+
if (!ttlKey && cached.ttl !== effectiveTTL) {
|
|
265
267
|
console.error(
|
|
266
268
|
`WARNING: TTL mismatch for key: ${effectiveKey}. It is recommended to use the same TTL for the same key.`
|
|
267
269
|
);
|
|
@@ -269,6 +271,10 @@ var PromiseCache = class {
|
|
|
269
271
|
return cached.value;
|
|
270
272
|
}
|
|
271
273
|
const response = await delegate();
|
|
274
|
+
if (ttlKey) {
|
|
275
|
+
const responseDict = response;
|
|
276
|
+
effectiveTTL = Number(responseDict[ttlKey]) || effectiveTTL;
|
|
277
|
+
}
|
|
272
278
|
this.persistor.set(effectiveKey, {
|
|
273
279
|
value: response,
|
|
274
280
|
timestamp: now,
|