Skip to content

Commit

Permalink
Refactor cacheFunc and Cache constructor to accept optional options
Browse files Browse the repository at this point in the history
  • Loading branch information
shtse8 committed Mar 22, 2024
1 parent 8c659d3 commit 683371e
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export interface CacheOptions {
* }
* }
*/
export function cacheFunc<T, Args extends readonly unknown[]>(fn: (...args: Args) => Promise<T>, options: CacheOptions) {
export function cacheFunc<T, Args extends readonly unknown[]>(fn: (...args: Args) => Promise<T>, options?: CacheOptions) {
const cache = new Cache<T>(options)
return (...args: Args) => cache.run(fn)
}
Expand All @@ -59,9 +59,9 @@ export class Cache<T> {
private value: T | undefined
private updatedAt: Date | undefined
private ttl: number;
constructor(options: CacheOptions) {
this.ttl = options.ttl || 1000 * 60 * 60
options.invalidator?.on('invalidate', () => this.invalidate())
constructor(options?: CacheOptions) {
this.ttl = options?.ttl || 1000 * 60 * 60
options?.invalidator?.on('invalidate', () => this.invalidate())
}

set(value: T) {
Expand Down

0 comments on commit 683371e

Please sign in to comment.