Function createRedisCacheFetcher

  • Wraps a function, that fetches and caches a value, using a RedisCache instance. The signature of the original function will be kept.

    Type Parameters

    • TFunc extends AsyncFunc

    Parameters

    • redis: RedisCache

      The redis cache instance.

    • key: string

      The key inside the redis instance, where the fetched value will be stored.

    • fetcher: TFunc

      The original function, that fetches the data.

    • Optional options: Nilable<ICreateRedisCacheFetcherOptions>

      Custom options.

    Returns RedisCacheFetcher<TFunc>

    The new, wrapped function.

    Example

    import axios from "axios"
    import RedisCache from "@egomobile/redis"

    const cache = new RedisCache()

    const loadRandomUsers = createRedisCacheFetcher(
    cache,
    'randomUsersKey',
    async (seed: string) => {
    const response = await axios.get(`https://randomuser.me/api/?seed=${encodeURIComponent(seed)}`)

    return response.data
    }
    )

    // first call MUST be successful, otherwise
    // exception is re-thrown
    const data1 = await loadRandomUsers("foobar1")

    // should be same as `data1`, because it is cached
    const data2 = await loadRandomUsers("foobar2")

    // reset and force reloading data
    await loadRandomUsers.reset()
    const data3 = await loadRandomUsers("foobar3")

Generated using TypeDoc