Last active
December 11, 2024 11:23
-
-
Save jacobparis/9c8a45bf9e3eedf81f0f44f9286c1190 to your computer and use it in GitHub Desktop.
Cachified
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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