Last active
December 1, 2023 17:24
-
-
Save crazy4groovy/1f711f2db7043663ea5f2976f98fc709 to your computer and use it in GitHub Desktop.
Revisions
-
crazy4groovy revised this gist
Dec 1, 2023 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -19,7 +19,7 @@ export function cacher<T>(thunk: () => Promise<T>, ttlSeconds = 60) { // This cache repo is really just a map of cache keys to cachers // callback is the function to cache, props are the call arguments export function cacheRepo<Props, Result, This = any>( callback: (this: This, props: Props) => Promise<Result>, ttlSeconds = 60 ) { @@ -39,7 +39,7 @@ export function cacheRepo<This, Props, Result>( repo.set(cacheKey, myCacher); setTimeout(() => { // Note: this critical delete step helps minimize the cache mem size, avoid mem leaks repo.delete(cacheKey); }, 1000 * ttlSeconds); -
crazy4groovy revised this gist
Dec 1, 2023 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -18,6 +18,7 @@ export function cacher<T>(thunk: () => Promise<T>, ttlSeconds = 60) { } // This cache repo is really just a map of cache keys to cachers // callback is the function to cache, props are the call arguments export function cacheRepo<This, Props, Result>( callback: (this: This, props: Props) => Promise<Result>, ttlSeconds = 60 -
crazy4groovy revised this gist
Dec 1, 2023 . No changes.There are no files selected for viewing
-
crazy4groovy created this gist
Sep 19, 2023 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,47 @@ export function cacher<T>(thunk: () => Promise<T>, ttlSeconds = 60) { const noCache: unique symbol = Symbol("cache"); let cache: Promise<T> | symbol = noCache; return function execThunk(): Promise<T> { if (typeof cache !== "symbol") { return cache; } cache = thunk(); setTimeout(() => { cache = noCache; }, 1000 * ttlSeconds); return cache; }; } // This cache repo is really just a map of cache keys to cachers export function cacheRepo<This, Props, Result>( callback: (this: This, props: Props) => Promise<Result>, ttlSeconds = 60 ) { const repo = new Map<string, () => Promise<Result>>(); return function execCallback(this: This, props: Props): Promise<Result> { const cacheKey = JSON.stringify(props); let myCacher = repo.get(cacheKey); if (myCacher) { return myCacher(); } const myThunk = () => callback.call(this, props); myCacher = cacher(myThunk, ttlSeconds); repo.set(cacheKey, myCacher); setTimeout(() => { // Note: this trimming step is important to keep our cache mem size in check repo.delete(cacheKey); }, 1000 * ttlSeconds); return myCacher(); }; }