cachenado v1.0.0
Cachenado
Cachenado is a small caching utility which allows you to connect a store / stage manager and function results. There is no true cache included in this library. Instead Cachenado has an included cache which map references to your store key with functions hash.
Install
yarn add cachenado
How it works
Get a cache
First, you need a cache. Cachenado provides a utility to create it as follow :
// Let's say that it's my store
const myVariableStore = {
applicationUserList: [],
};
// I map the "getUserList" function to the "applicationUserList" key
const funcNameToObservableNameMapping: { [key: string]: keyof typeof myVariableStore } = {
getUserList: "applicationUserList",
};
// And I create the cache
const cache = createCache(myVariableStore, funcNameToObservableNameMapping);
Or you can implement your own cache if you need to connect it to your store in a different way, it need to implements the following two functions :
get: (key: string) => { value: any; until: number } | undefined;
set: (key: string, obj: { value: any; until: number }) => any;
I plan to implement utility functions to createCache for Mobx or Redux stores.
Add caching to your function
const getUserList = () => {
return [{ name: "Michel" }, { name: "Michoul" }];
};
const augmentedGetUserList = timeCacheResult(cache, getUserList, {
cacheTime: 5000, // In ms. Optionnal, default 10000
nameToCache: "getUsers", // Optionnal, default is the cached function name
});
The second call to this function within 10 seconds and with the same arguments will instead use the get function provided by the cache to get the value stored. It will not call getUserList
again.
License
4 years ago