Skip to content

Commit

Permalink
Remove the ability of turning off superjson (#40)
Browse files Browse the repository at this point in the history
* Remove the ability of turning off superjson

* fixes types
  • Loading branch information
PabloSzx committed Mar 9, 2023
1 parent 2bbc5a8 commit 6120a4a
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 92 deletions.
5 changes: 5 additions & 0 deletions .changeset/early-buttons-yell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@soundxyz/fine-grained-cache": major
---

Remove the ability of turning off superjson
36 changes: 6 additions & 30 deletions src/fineGrained.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,6 @@ export function FineGrainedCache({

async function getRedisCacheValue<T>(
key: string,
useSuperjson: boolean,
checkShortMemoryCache: boolean
): Promise<T | typeof NotFoundSymbol> {
const tracing =
Expand Down Expand Up @@ -473,9 +472,7 @@ export function FineGrainedCache({
let parsedRedisValue: Awaited<T>;

try {
parsedRedisValue = useSuperjson
? superjson.parse<Awaited<T>>(redisValue)
: (JSON.parse(redisValue) as Awaited<T>);
parsedRedisValue = superjson.parse<Awaited<T>>(redisValue);
} catch (err) {
onError(new Error(`Unexpected JSON string for ${key}`));

Expand Down Expand Up @@ -503,7 +500,6 @@ export function FineGrainedCache({
maxExpectedTime = defaultMaxExpectedTime,
retryLockTime = defaultRetryLockTime,
checkShortMemoryCache = defaultUseMemoryCache,
useSuperjson = true,
useRedlock = useRedlockByDefault,
forceUpdate = false,
}: {
Expand All @@ -520,10 +516,6 @@ export function FineGrainedCache({
* @default true
*/
checkShortMemoryCache?: boolean;
/**
* @default true
*/
useSuperjson?: boolean;
/**
* @default false
*/
Expand Down Expand Up @@ -558,11 +550,7 @@ export function FineGrainedCache({
return ConcurrentCachedCall(key, async () => {
if (forceUpdate) return getNewValue();

const redisValue = await getRedisCacheValue<Awaited<T>>(
key,
useSuperjson,
checkShortMemoryCache
);
const redisValue = await getRedisCacheValue<Awaited<T>>(key, checkShortMemoryCache);

if (redisValue !== NotFoundSymbol) return redisValue;

Expand Down Expand Up @@ -622,7 +610,6 @@ export function FineGrainedCache({

const redisValueAfterLock = await getRedisCacheValue<Awaited<T>>(
key,
useSuperjson,
checkShortMemoryCache
);

Expand Down Expand Up @@ -690,10 +677,7 @@ export function FineGrainedCache({
? getRemainingSeconds(timedInvalidationDate)
: ttlSeconds;

const stringifiedValue = useSuperjson
? superjson.stringify(newValue)
: JSON.stringify(newValue);

const stringifiedValue = superjson.stringify(newValue);
if (expirySeconds > 0) {
if (pipelineRedisSET) {
const set = pipelinedRedisSet({
Expand Down Expand Up @@ -809,20 +793,18 @@ export function FineGrainedCache({
populateMemoryCache = defaultUseMemoryCache,
ttl,
keys,
useSuperjson,
value,
}: {
populateMemoryCache: boolean;
ttl: StringValue | "Infinity";
keys: string | [string, ...(string | number)[]];
useSuperjson: boolean;
value: T;
}) {
const key = generateCacheKey(keys);

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

const stringifiedValue = useSuperjson ? superjson.stringify(value) : JSON.stringify(value);
const stringifiedValue = superjson.stringify(value);

if (expirySeconds > 0) {
if (populateMemoryCache) memoryCache.set(key, value);
Expand Down Expand Up @@ -870,16 +852,10 @@ export function FineGrainedCache({
}
}

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

return getRedisCacheValue<Awaited<T>>(key, useSuperjson, false).then((value) => {
return getRedisCacheValue<Awaited<T>>(key, false).then((value) => {
if (value === NotFoundSymbol) {
return {
found: false,
Expand Down
62 changes: 0 additions & 62 deletions test/fineGrained.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ test("fine grained - without memory cache and invalidate pattern", async (t) =>
const data = await getCached(cb, {
keys: ["test", 1],
ttl: "10 seconds",
useSuperjson: false,
});

t.is(data, "hello world");
Expand All @@ -123,7 +122,6 @@ test("fine grained - without memory cache and invalidate pattern", async (t) =>
const data2 = await getCached(cb, {
keys: ["test", 1],
ttl: "10 seconds",
useSuperjson: false,
});

t.is(data2, data);
Expand All @@ -134,7 +132,6 @@ test("fine grained - without memory cache and invalidate pattern", async (t) =>
const data3 = await getCached(cb, {
keys: "test",
ttl: "10 seconds",
useSuperjson: false,
});

t.is(data3, data);
Expand Down Expand Up @@ -647,7 +644,6 @@ test("setCache - regular", async (t) => {
{
const cache = await readCache({
keys,
useSuperjson: false,
});

t.deepEqual<typeof cache, typeof cache>(cache, {
Expand All @@ -660,7 +656,6 @@ test("setCache - regular", async (t) => {
await setCache({
keys,
ttl,
useSuperjson: false,
value,
populateMemoryCache: false,
});
Expand All @@ -672,7 +667,6 @@ test("setCache - regular", async (t) => {
{
keys,
ttl,
useSuperjson: false,
}
);

Expand All @@ -681,58 +675,6 @@ test("setCache - regular", async (t) => {
{
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({
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);

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

t.deepEqual<typeof cache, typeof cache>(cache, {
Expand All @@ -751,8 +693,6 @@ test("setCache - memory cache", async (t) => {
await setCache({
keys,
ttl,
// On purpose mismatch, memory cache should override
useSuperjson: true,
value,
populateMemoryCache: true,
});
Expand All @@ -764,8 +704,6 @@ test("setCache - memory cache", async (t) => {
{
keys,
ttl,
// On purpose mismatch, memory cache should override
useSuperjson: false,
checkShortMemoryCache: true,
}
);
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
// "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */

"noEmit": true,
/* Modules */
"module": "esnext" /* Specify what module code is generated. */,
// "rootDir": "./", /* Specify the root folder within your source files. */
Expand Down

0 comments on commit 6120a4a

Please sign in to comment.