Skip to content

Commit

Permalink
feat(cache): stringify incoming data when adding to cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael committed Oct 13, 2022
1 parent a9eeff8 commit 6ab6fba
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions lib/ttl-cache/ttl-cache.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { environment } from "../urbex";
import { isUndefined, isNegative, absolute } from "../utils";
import { isUndefined, isNegative, absolute, assignOptions } from "../utils";
import { timeProvider } from "./time-provider";
import { CacheClock } from "./cache-clock";
import { hash } from "./hash";
Expand All @@ -26,10 +26,6 @@ const defaultCacheOptions: CacheOptions = {
ttl: Infinity
};

function assignOptions<P, T>(defaultOptions: P, options: T): P & T {
return Object.assign({}, defaultOptions, options);
}

function parseOptions(
options: Partial<CacheOptions> = {},
defaultOptions: CacheOptions
Expand Down Expand Up @@ -90,7 +86,7 @@ export class TTLCache {
const { ttl } = parseOptions(options, this.options);

const item: CacheItem = {
value: value,
value: JSON.stringify(value),
expires: timeProvider.now() + ttl
};

Expand All @@ -109,7 +105,11 @@ export class TTLCache {
return undefined;
}

return item.value;
try {
return JSON.parse(item.value);
} catch {
return item.value;
}
}

has(key: string): boolean {
Expand Down

0 comments on commit 6ab6fba

Please sign in to comment.