Skip to content

Commit

Permalink
new "setCache"
Browse files Browse the repository at this point in the history
  • Loading branch information
PabloSzx committed Nov 1, 2022
1 parent d03ea77 commit 9741c97
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/shaggy-shirts-chew.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@soundxyz/fine-grained-cache": minor
---

New "setCache" function returned alongside "getCached"
66 changes: 66 additions & 0 deletions src/fineGrained.ts
Original file line number Diff line number Diff line change
Expand Up @@ -792,11 +792,77 @@ export function FineGrainedCache({
}
}

async function setCache({
populateMemoryCache = defaultUseMemoryCache,
ttl,
keys,
useSuperjson,
value,
}: {
populateMemoryCache: boolean;
ttl: StringValue | "Infinity";
keys: string | [string, ...(string | number)[]];
useSuperjson: boolean;
value: unknown;
}) {
const key = generateCacheKey(keys);

const expirySeconds = ttl === "Infinity" ? -1 : getExpirySeconds(ttl);

const stringifiedValue = useSuperjson ? superjson.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 {
getCached,
generateCacheKey,
keyPrefix,
memoryCache,
invalidateCache,
setCache,
};
}
89 changes: 88 additions & 1 deletion test/fineGrained.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import test from "ava";
import { join } from "path";
import { CachedCallback, FineGrainedCache, LogEventArgs } from "../src";
import { getCached, invalidateCache, logEverything, memoryCache, redis } from "./utils";
import { getCached, invalidateCache, logEverything, memoryCache, redis, setCache } from "./utils";
import { createDeferredPromise } from "../src/utils";
import { setTimeout } from "timers/promises";
import { addMinutes, minutesToSeconds } from "date-fns";
Expand Down Expand Up @@ -631,3 +631,90 @@ test("pipelined sets", async (t) => {

t.deepEqual(valuesOnGet, [111, 222]);
});

test("setCache - regular", async (t) => {
const keys = "test";
const ttl = "10 seconds" as const;

const value = 123;

await setCache({
keys,
ttl,
useSuperjson: false,
value,
populateMemoryCache: false,
});

const data = await getCached<number>(
() => {
throw Error("Unexpected missing data");
},
{
keys,
ttl,
useSuperjson: false,
}
);

t.is(data, value);
});

test("setCache - superjson", async (t) => {
const keys = "test";
const ttl = "10 seconds" as const;

const value = 456;

await setCache({
keys,
ttl,
useSuperjson: true,
value,
populateMemoryCache: false,
});

const data = await getCached<number>(
() => {
throw Error("Unexpected missing data");
},
{
keys,
ttl,
useSuperjson: true,
}
);

t.is(data, value);
});

test("setCache - memory cache", async (t) => {
const keys = "test";
const ttl = "10 seconds" as const;

const value = 456;

await setCache({
keys,
ttl,
// On purpose mismatch, memory cache should override
useSuperjson: true,
value,
populateMemoryCache: true,
});

const data = await getCached<number>(
() => {
throw Error("Unexpected missing data");
},
{
keys,
ttl,
// On purpose mismatch, memory cache should override
useSuperjson: false,
checkShortMemoryCache: true,
}
);

t.is(data, value);
});
2 changes: 1 addition & 1 deletion test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const redis = new Redis({
port: 6389,
});

export const { memoryCache, getCached, invalidateCache, generateCacheKey, keyPrefix } =
export const { memoryCache, getCached, invalidateCache, generateCacheKey, keyPrefix, setCache } =
FineGrainedCache({
redis,
logEvents: {
Expand Down

0 comments on commit 9741c97

Please sign in to comment.