Fast GM Cache System

Lightweight cache with TTL using GM_setValue

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

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

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

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

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @name         Fast GM Cache System
// @namespace    fast.cache.system
// @version      1.0
// @description  Lightweight cache with TTL using GM_setValue
// @match        *://*/*
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_deleteValue
// @license      MIT
// ==/UserScript==

(function () {
    'use strict';

    const CACHE_PREFIX = "fastcache_";

    const Cache = {

        // 📦 Сохранить значение с временем жизни (в секундах)
        set(key, value, ttlSeconds = 300) {
            const data = {
                value: value,
                expiry: Date.now() + ttlSeconds * 1000
            };
            GM_setValue(CACHE_PREFIX + key, JSON.stringify(data));
        },

        // 📥 Получить значение (null если просрочено)
        get(key) {
            const raw = GM_getValue(CACHE_PREFIX + key, null);
            if (!raw) return null;

            try {
                const data = JSON.parse(raw);

                if (Date.now() > data.expiry) {
                    GM_deleteValue(CACHE_PREFIX + key);
                    return null;
                }

                return data.value;

            } catch {
                GM_deleteValue(CACHE_PREFIX + key);
                return null;
            }
        },

        // 🗑 Удалить вручную
        delete(key) {
            GM_deleteValue(CACHE_PREFIX + key);
        },

        // 🧹 Очистить всё
        clearAll() {
            const keys = Object.keys(localStorage);
            keys.forEach(k => {
                if (k.startsWith(CACHE_PREFIX)) {
                    GM_deleteValue(k);
                }
            });
        }
    };

    // ====== ПРИМЕР ИСПОЛЬЗОВАНИЯ ======

    const cached = Cache.get("userData");

    if (cached) {
        console.log("Из кеша:", cached);
    } else {
        console.log("Создаём новые данные");

        const newData = {
            id: 123,
            name: "FastUser"
        };

        Cache.set("userData", newData, 600); // 10 минут
    }

})();