cache-fns
v0.0.3
Published
Something with cache ig. Utilities for Caching / Data Fallback Handling
Downloads
5
Maintainers
Readme
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
LICENSE
This package is licensed under the GNU Lesser General Public License.