Smart Shared Cache System

Shared cache with TTL, background refresh and tab sync

スクリプトをインストールするには、Tampermonkey, GreasemonkeyViolentmonkey のような拡張機能のインストールが必要です。

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

スクリプトをインストールするには、TampermonkeyViolentmonkey のような拡張機能のインストールが必要です。

スクリプトをインストールするには、TampermonkeyUserscripts のような拡張機能のインストールが必要です。

このスクリプトをインストールするには、Tampermonkeyなどの拡張機能をインストールする必要があります。

このスクリプトをインストールするには、ユーザースクリプト管理ツールの拡張機能をインストールする必要があります。

(ユーザースクリプト管理ツールは設定済みなのでインストール!)

このスタイルをインストールするには、Stylusなどの拡張機能をインストールする必要があります。

このスタイルをインストールするには、Stylus などの拡張機能をインストールする必要があります。

このスタイルをインストールするには、Stylus tなどの拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

(ユーザースタイル管理ツールは設定済みなのでインストール!)

このスクリプトの質問や評価の投稿はこちら通報はこちらへお寄せください
// ==UserScript==
// @name         Smart Shared Cache System
// @namespace    smart.cache.shared
// @version      2.0
// @description  Shared cache with TTL, background refresh and tab sync
// @match        *://*/*
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_deleteValue
// @grant        GM_listValues
// @run-at       document-start
// @license      MIT
// ==/UserScript==

(function () {
    'use strict';

    const PREFIX = "GLOBAL_CACHE_";
    const channel = new BroadcastChannel("GLOBAL_CACHE_SYNC");

    const Cache = {

        // 📦 set with TTL
        set(key, value, ttl = 300) {
            const data = {
                value,
                expiry: Date.now() + ttl * 1000
            };

            GM_setValue(PREFIX + key, JSON.stringify(data));

            // уведомляем другие вкладки
            channel.postMessage({ type: "update", key });
        },

        // 📥 get with auto cleanup
        get(key) {
            const raw = GM_getValue(PREFIX + key, null);
            if (!raw) return null;

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

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

                return data.value;

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

        // 🔄 get with background refresh
        async getOrRefresh(key, ttl, fetchFunction) {

            const existing = this.get(key);

            if (existing) return existing;

            // если нет — загружаем в фоне
            const fresh = await fetchFunction();
            this.set(key, fresh, ttl);
            return fresh;
        },

        // 🗑 удалить
        delete(key) {
            GM_deleteValue(PREFIX + key);
            channel.postMessage({ type: "delete", key });
        },

        // 🧹 очистить всё
        clearAll() {
            GM_listValues().forEach(k => {
                if (k.startsWith(PREFIX)) {
                    GM_deleteValue(k);
                }
            });

            channel.postMessage({ type: "clear" });
        }
    };

    // 🔔 Синхронизация вкладок
    channel.onmessage = (event) => {
        const msg = event.data;
        if (!msg) return;

        if (msg.type === "update") {
            console.log("Cache updated in another tab:", msg.key);
        }

        if (msg.type === "delete") {
            console.log("Cache deleted in another tab:", msg.key);
        }

        if (msg.type === "clear") {
            console.log("Cache cleared in another tab");
        }
    };

    // ====== Пример использования ======

    async function example() {
        const data = await Cache.getOrRefresh(
            "apiData",
            600,
            async () => {
                console.log("Fetching fresh data...");
                const response = await fetch("https://api.example.com/data");
                return await response.json();
            }
        );

        console.log("Data:", data);
    }

    // example(); // раскомментируй при необходимости

})();