@soundxyz/fine-grained-cache
Advanced tools
Comparing version 2.2.1 to 2.3.0
@@ -132,3 +132,10 @@ import type { Redis } from "ioredis"; | ||
invalidateCache: (keys_0: string, ...keys_1: (string | number)[]) => Promise<void>; | ||
setCache: ({ populateMemoryCache, ttl, keys, useSuperjson, value, }: { | ||
populateMemoryCache: boolean; | ||
ttl: StringValue | "Infinity"; | ||
keys: string | [string, ...(string | number)[]]; | ||
useSuperjson: boolean; | ||
value: unknown; | ||
}) => Promise<void>; | ||
}; | ||
export {}; |
@@ -500,2 +500,55 @@ 'use strict'; | ||
} | ||
async function setCache({ | ||
populateMemoryCache = defaultUseMemoryCache, | ||
ttl, | ||
keys, | ||
useSuperjson, | ||
value | ||
}) { | ||
const key = generateCacheKey(keys); | ||
const expirySeconds = ttl === "Infinity" ? -1 : getExpirySeconds(ttl); | ||
const stringifiedValue = useSuperjson ? superjson__default["default"].stringify(value) : JSON.stringify(value); | ||
if (expirySeconds > 0) { | ||
if (populateMemoryCache) | ||
memoryCache.set(key, value); | ||
if (pipelineRedisSET) { | ||
await pipelinedRedisSet({ | ||
key, | ||
value: stringifiedValue, | ||
ttl: expirySeconds | ||
}); | ||
} else { | ||
const tracing = enabledLogEvents?.REDIS_SET ? getTracing() : null; | ||
await redis.setex(key, expirySeconds, stringifiedValue).then(() => { | ||
if (tracing) { | ||
logMessage("REDIS_SET", { | ||
key, | ||
expirySeconds, | ||
time: tracing() | ||
}); | ||
} | ||
}); | ||
} | ||
} else if (ttl === "Infinity") { | ||
if (populateMemoryCache) | ||
memoryCache.set(key, value); | ||
if (pipelineRedisSET) { | ||
await pipelinedRedisSet({ | ||
key, | ||
value: stringifiedValue | ||
}); | ||
} else { | ||
const tracing = enabledLogEvents?.REDIS_SET ? getTracing() : null; | ||
await redis.set(key, stringifiedValue).then(() => { | ||
if (tracing) { | ||
logMessage("REDIS_SET", { | ||
key, | ||
expirySeconds: "Infinity", | ||
time: tracing() | ||
}); | ||
} | ||
}); | ||
} | ||
} | ||
} | ||
return { | ||
@@ -506,3 +559,4 @@ getCached, | ||
memoryCache, | ||
invalidateCache | ||
invalidateCache, | ||
setCache | ||
}; | ||
@@ -509,0 +563,0 @@ } |
{ | ||
"name": "@soundxyz/fine-grained-cache", | ||
"version": "2.2.1", | ||
"version": "2.3.0", | ||
"description": "Fine-grained cache helper using redis", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
Sorry, the diff of this file is not supported yet
49101
1379