@soundxyz/fine-grained-cache
Advanced tools
Comparing version 1.0.1 to 1.1.0
@@ -10,2 +10,15 @@ import type { Redis } from "ioredis"; | ||
} | ||
export declare type CachedCallback<T> = (options: { | ||
setTTL(options: { | ||
/** | ||
* Set TTL to `null` to disable caching | ||
*/ | ||
ttl?: StringValue | "Infinity" | null; | ||
timedInvalidation?: null | Date | (() => Date | Promise<Date>); | ||
}): void; | ||
getTTL(): { | ||
ttl: StringValue | "Infinity" | null; | ||
timedInvalidation: undefined | null | Date | (() => Date | Promise<Date>); | ||
}; | ||
}) => T; | ||
export declare function FineGrainedCache({ redis, redLock: redLockConfig, keyPrefix, memoryCache, onError, }: { | ||
@@ -26,3 +39,3 @@ redis: Redis; | ||
}): { | ||
getCached: <T>(cb: () => T, { timedInvalidation, ttl, keys, maxExpectedTime, retryLockTime, checkShortMemoryCache, useSuperjson, useRedlock, forceUpdate, }: { | ||
getCached: <T>(cb: CachedCallback<T>, { timedInvalidation, ttl, keys, maxExpectedTime, retryLockTime, checkShortMemoryCache, useSuperjson, useRedlock, forceUpdate, }: { | ||
timedInvalidation?: Date | (() => Date | Promise<Date>) | undefined; | ||
@@ -29,0 +42,0 @@ ttl: StringValue | "Infinity"; |
@@ -113,7 +113,21 @@ 'use strict'; | ||
async function getNewValue() { | ||
const newValue = await cb(); | ||
let currentTTL = ttl; | ||
let currentTimedInvalidation = timedInvalidation; | ||
let expirySeconds = 1; | ||
const newValue = await cb({ | ||
setTTL(options) { | ||
currentTTL = options.ttl !== void 0 ? options.ttl : currentTTL; | ||
currentTimedInvalidation = options.timedInvalidation !== void 0 ? options.timedInvalidation : currentTimedInvalidation; | ||
}, | ||
getTTL() { | ||
return { | ||
ttl: currentTTL, | ||
timedInvalidation: currentTimedInvalidation | ||
}; | ||
} | ||
}); | ||
try { | ||
const timedInvalidationDate = timedInvalidation ? typeof timedInvalidation === "function" ? await timedInvalidation() : timedInvalidation : null; | ||
const ttlSeconds = ttl === "Infinity" ? -1 : getExpirySeconds(ttl); | ||
const expirySeconds = timedInvalidationDate && timedInvalidationDate.getTime() > Date.now() ? utils.getRemainingSeconds(timedInvalidationDate) : ttlSeconds; | ||
const timedInvalidationDate = currentTimedInvalidation ? typeof currentTimedInvalidation === "function" ? await currentTimedInvalidation() : currentTimedInvalidation : null; | ||
const ttlSeconds = currentTTL == null ? 0 : currentTTL === "Infinity" ? -1 : getExpirySeconds(currentTTL); | ||
expirySeconds = timedInvalidationDate && timedInvalidationDate.getTime() > Date.now() ? utils.getRemainingSeconds(timedInvalidationDate) : ttlSeconds; | ||
const stringifiedValue = useSuperjson ? superjson__default["default"].stringify(newValue) : JSON.stringify(newValue); | ||
@@ -128,3 +142,3 @@ if (expirySeconds > 0) { | ||
} | ||
if (checkShortMemoryCache) | ||
if (expirySeconds > 0 && checkShortMemoryCache) | ||
memoryCache.set(key, newValue); | ||
@@ -131,0 +145,0 @@ return newValue; |
{ | ||
"name": "@soundxyz/fine-grained-cache", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "Fine-grained cache helper using redis", | ||
@@ -49,2 +49,3 @@ "keywords": [ | ||
"ioredis": "^5.0.6", | ||
"prettier": "^2.6.2", | ||
"redlock": "5.0.0-beta.2", | ||
@@ -51,0 +52,0 @@ "typescript": "^4.7.3" |
Sorry, the diff of this file is not supported yet
17517
397
13