Skip to content

Instantly share code, notes, and snippets.

@crazy4groovy
Last active December 1, 2023 17:24
Show Gist options
  • Select an option

  • Save crazy4groovy/1f711f2db7043663ea5f2976f98fc709 to your computer and use it in GitHub Desktop.

Select an option

Save crazy4groovy/1f711f2db7043663ea5f2976f98fc709 to your computer and use it in GitHub Desktop.

Revisions

  1. crazy4groovy revised this gist Dec 1, 2023. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions cacher.ts
    Original 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<This, Props, Result>(
    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 trimming step is important to keep our cache mem size in check
    // Note: this critical delete step helps minimize the cache mem size, avoid mem leaks
    repo.delete(cacheKey);
    }, 1000 * ttlSeconds);

  2. crazy4groovy revised this gist Dec 1, 2023. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions cacher.ts
    Original 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
  3. crazy4groovy revised this gist Dec 1, 2023. No changes.
  4. crazy4groovy created this gist Sep 19, 2023.
    47 changes: 47 additions & 0 deletions cacher.ts
    Original 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();
    };
    }