There are 2 ways of caching data with this class either globally or locally. you can cache data gobally by using the Cache constant you can import from the cache.js file if you want to cache data locally you can use the DataCacher class also imported from the cache.js file.
When you want to cache data locally you should import the DataCacher class from the cache.js file and create a new instance of the class like shown below this caches within the module (file/script) and can't be shared across files unless exported
import { DataCacher } from './utils/cache';
const Cache = new DataCacher();
The default export Cache is a instance of the DataCacher class that has been instanciated within the cache.js file this way the cached data can be shared across diffrent modules (files/scripts).
import Cache from './utils/cache';
prop | value | use case |
---|---|---|
temp | Array | temp data storage |
data | Object | cached data storage |
state | Boolean | true when processing temp data |
-
add
With this method you can add data to the caching queue
Info
call async return value Cache.add() no none Parameters
param value optional catagory String no index String no data Object no Example
Cache.add("users", userdata.index, userdata)
-
get
With this method you can fetch cached data
Info
call async return value Cache.get() yes data Parameters
param value optional catagory String no index String no callback Function yes Example
// without callback // option 1 const userdata = Cache.get("users", "sdoa812bdo8d0a8h081"); // option 2 const userdata = await Cache.get("users", "sdoa812bdo8d0a8h081"); // with callback Cache.get("users", "sdoa812bdo8d0a8h081", (userdata) => { console.log(userdata) })