0.0.3 • Published 5 months ago

cache-fns v0.0.3

Weekly downloads
-
License
LGPL-3.0
Repository
-
Last release
5 months ago

Something with cache ig. Utilities for Caching / Data Fallback Handling.

Table of Contents

Installation

Using npm:

npm install cache-fns

or if you prefer to use the yarn package manager:

yarn add cache-fns

or if you prefer to use the pnpm package manager:

pnpm add cache-fns

Documentation

useCache

useCache takes in as it's first parameter the key that will be sent to getter and setter functions to retrieve/store data.

Further arguments will be used in order to determine how the data is stored and retrieved.

In the example bellow, myLocalCache and myRemoteCache are imaginary caches that we use to demonstrate the function.

const data = await useCache<OurReturnType>(
    'myKey',
    // Resolver with both getter and setter
    {
        get: async (key) => {
            // Get the key from our local cache
            return await myLocalCache.get(key);
        },
        set: async (key, value) => {
            // Set the key in our local cache
            await myLocalCache.set(key, value);
        },
    },
    // Resolver with only getter
    async (key) => {
        // Get the key from our remote cache
        return await myRemoteCache.get(key);
    }
);

Contributors

npm.io

LICENSE

This package is licensed under the GNU Lesser General Public License.