Skip to content

Commit

Permalink
New "readCache" that reads directly from redis cache (#38)
Browse files Browse the repository at this point in the history
* New "readCache" that reads directly from redis

* add test

* unknown T

* fix found: true

* improve tests
  • Loading branch information
PabloSzx authored Feb 16, 2023
1 parent ae354bd commit c0854d2
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/slimy-carrots-prove.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@soundxyz/fine-grained-cache": minor
---

New "readCache" that reads directly from redis cache
30 changes: 27 additions & 3 deletions src/fineGrained.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export const Events = {
REDLOCK_GET_AFTER_ACQUIRE: "REDLOCK_GET_AFTER_ACQUIRE",
} as const;

export type Events = typeof Events[keyof typeof Events];
export type Events = (typeof Events)[keyof typeof Events];

export type EventParamsObject = Record<string, string | number | boolean | null | undefined>;

Expand Down Expand Up @@ -805,7 +805,7 @@ export function FineGrainedCache({
}
}

async function setCache({
async function setCache<T = unknown>({
populateMemoryCache = defaultUseMemoryCache,
ttl,
keys,
Expand All @@ -816,7 +816,7 @@ export function FineGrainedCache({
ttl: StringValue | "Infinity";
keys: string | [string, ...(string | number)[]];
useSuperjson: boolean;
value: unknown;
value: T;
}) {
const key = generateCacheKey(keys);

Expand Down Expand Up @@ -870,12 +870,36 @@ export function FineGrainedCache({
}
}

function readCache<T = unknown>({
keys,
useSuperjson,
}: {
keys: string | [string, ...(string | number)[]];
useSuperjson: boolean;
}) {
const key = generateCacheKey(keys);

return getRedisCacheValue<Awaited<T>>(key, useSuperjson, false).then((value) => {
if (value === NotFoundSymbol) {
return {
found: false,
} as const;
}

return {
found: true,
value,
} as const;
});
}

return {
getCached,
generateCacheKey,
keyPrefix,
memoryCache,
invalidateCache,
setCache,
readCache,
};
}
56 changes: 55 additions & 1 deletion test/fineGrained.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import test from "ava";
import { join } from "path";
import { CachedCallback, FineGrainedCache, LogEventArgs } from "../src";
import { getCached, invalidateCache, logEverything, memoryCache, redis, setCache } from "./utils";
import {
getCached,
invalidateCache,
logEverything,
memoryCache,
readCache,
redis,
setCache,
} from "./utils";
import { createDeferredPromise } from "../src/utils";
import { setTimeout } from "timers/promises";
import { addMinutes, minutesToSeconds } from "date-fns";
Expand Down Expand Up @@ -636,6 +644,17 @@ test("setCache - regular", async (t) => {
const keys = "test";
const ttl = "10 seconds" as const;

{
const cache = await readCache({
keys,
useSuperjson: false,
});

t.deepEqual<typeof cache, typeof cache>(cache, {
found: false,
});
}

const value = 123;

await setCache({
Expand All @@ -658,12 +677,35 @@ test("setCache - regular", async (t) => {
);

t.is(data, value);

{
const cache = await readCache({
keys,
useSuperjson: false,
});

t.deepEqual<typeof cache, typeof cache>(cache, {
found: true,
value,
});
}
});

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

{
const cache = await readCache({
keys,
useSuperjson: true,
});

t.deepEqual<typeof cache, typeof cache>(cache, {
found: false,
});
}

const value = 456;

await setCache({
Expand All @@ -686,6 +728,18 @@ test("setCache - superjson", async (t) => {
);

t.is(data, value);

{
const cache = await readCache({
keys,
useSuperjson: true,
});

t.deepEqual<typeof cache, typeof cache>(cache, {
found: true,
value,
});
}
});

test("setCache - memory cache", async (t) => {
Expand Down
25 changes: 16 additions & 9 deletions test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,23 @@ export const redis = new Redis({
port: 6389,
});

export const { memoryCache, getCached, invalidateCache, generateCacheKey, keyPrefix, setCache } =
FineGrainedCache({
redis,
logEvents: {
log: console.log,
events: {
MEMORY_CACHE_HIT: true,
},
export const {
memoryCache,
getCached,
invalidateCache,
generateCacheKey,
keyPrefix,
setCache,
readCache,
} = FineGrainedCache({
redis,
logEvents: {
log: console.log,
events: {
MEMORY_CACHE_HIT: true,
},
});
},
});

export const logEverything: Required<LoggedEvents> = {
EXECUTION_TIME: true,
Expand Down

0 comments on commit c0854d2

Please sign in to comment.