Skip to content

Instantly share code, notes, and snippets.

@jacobparis
Last active December 11, 2024 11:23
Show Gist options
  • Save jacobparis/9c8a45bf9e3eedf81f0f44f9286c1190 to your computer and use it in GitHub Desktop.
Save jacobparis/9c8a45bf9e3eedf81f0f44f9286c1190 to your computer and use it in GitHub Desktop.
Cachified
import {
cachified as baseCachified,
type CacheEntry,
type Cache,
totalTtl,
type CachifiedOptions,
verboseReporter,
} from '@epic-web/cachified';
import { remember } from '@epic-web/remember';
import { LRUCache } from 'lru-cache';
/* lru cache is not part of this package but a simple non-persistent cache */
const lruInstance = remember('lru', () => new LRUCache<string, CacheEntry>({ max: 1000 }));
export const lru: Cache = {
set(key, value) {
const ttl = totalTtl(value?.metadata);
return lruInstance.set(key, value, {
ttl: ttl === Infinity ? undefined : ttl,
start: value?.metadata?.createdTime,
});
},
get(key) {
return lruInstance.get(key);
},
delete(key) {
return lruInstance.delete(key);
},
};
export function cachified<T>(options: Omit<CachifiedOptions<T>, 'cache'>) {
return baseCachified({ ...options, cache: lru }, verboseReporter());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment