qik.cache.js

import { LRUCache } from 'lru-cache';


///////////////////////////////////////////////////////////////////////////////

var caches = {};


/**
 * @classdesc A static service that provides tools for caching api requests and other information
 * @alias cache
 * @class
 * @hideconstructor
 */
var QikCache = {

    ///////////////////////////////////////////////////

    
    /**
     * A helper function to reset all caches, useful if changing organisation or logging in or out as another user     
     * @alias cache.reset    
     */
    reset() {
        Object.entries(caches).forEach(([key, cache]) => {
            if(cache.clear) {
                cache.clear()
            } else if(cache.reset) {
                cache.reset()
            }
        })
    },

    ///////////////////////////////////////////////////

    /**
     * A helper function to retrieve a specific cache  
     * @alias cache.get    
     * @param  {string} key The key for the cache you want to retrieve
     * @return {LRUCache} The cache store for the specified key
     */
    get(key, options) {
        options = options || {max:5};

        if (caches[key]) {
            return caches[key];
        }
        caches[key] = new LRUCache(options)
        return caches[key];
    }
}


///////////////////////////////////////////////////////////////////////////////

export default QikCache;