SimpleCache

A Map extended as a Cache. Optional KeyExtractor - Function, optional ValueTransformer - Functions, as well as an optional validityPeriod after which an entry is automatically going to be removed.

This script should not be not be installed directly. It is a library for other scripts to include with the meta directive // @require https://update.greasyfork.org/scripts/405143/815223/SimpleCache.js

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

You will need to install an extension such as Tampermonkey to install this script.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @name         SimpleCache
// @namespace    hoehleg.userscripts.private
// @version      0.1
// @description  A Map extended as a Cache. Optional KeyExtractor - Function, optional ValueTransformer - Functions, as well as an optional validityPeriod after which an entry is automatically going to be removed.
// @author       Gerrit Höhle
//
// @grant        none
//
// @require      https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js
// ==/UserScript==

/* jshint esnext: true */
/* globals $, jQuery */
const SimpleCache = (() => {
    const cloneObject = (obj) => {
        if (typeof obj !== 'object' && typeof obj !== 'function' || obj === null) {
            return obj;
        }
        return jQuery.extend(true, {}, obj);
    };

    return class SimpleCache extends Map {
        constructor({ keyExtractor = k => k, setValueTransformer = v => cloneObject(v), getValueTransformer = v => cloneObject(v) } = {}) {
            super();
            Object.assign(this, { keyExtractor, setValueTransformer, getValueTransformer });
        }

        get(key) {
            const finalKey = this.keyExtractor(key);
            return this.getValueTransformer(super.get(finalKey));
        }

        set(key, value, validityPeriodMs = 0) {
            const finalKey = this.keyExtractor(key);
            super.set(finalKey, this.setValueTransformer(value));

            if (validityPeriodMs > 0) {
                setTimeout(() => this.delete(finalKey), validityPeriodMs);
            }
            return this;
        }

        has(key) {
            return super.has(this.keyExtractor(key));
        }

        delete(key) {
            return super.delete(this.keyExtractor(key));
        }
    };
})();