5m1Ly / nodejs-data-cacher

A class to cache data in node js

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Caching

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.

Local Caching

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();

Global Caching

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';

Properties

prop value use case
temp Array temp data storage
data Object cached data storage
state Boolean true when processing temp data

Methods

  1. 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)
  2. 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)
    })

About

A class to cache data in node js

License:GNU General Public License v3.0


Languages

Language:JavaScript 100.0%