最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

緩存處理函數(shù)storageKeySuffix操作示例解析

 更新時間:2023年08月15日 14:22:20   作者:zhongjyuan  
這篇文章主要介紹了淺析緩存處理函數(shù)storageKeySuffix操作示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪

storageKeySuffix:生成存儲鍵的后綴字符串

/**
     * 生成存儲鍵的后綴字符串
     * @author zhongjyuan
     * @email zhongjyuan@outlook.com
     * @website http://zhongjyuan.club
     * @param {boolean} dynamic 是否使用動態(tài)后綴
     * @param {string} suffix 靜態(tài)后綴值,默認為 "zhongjyuan"
     * @returns {string} 存儲鍵的后綴字符串
     *
     * @example
     * storageKeySuffix(); // 假設當前窗口的端口號為 "8080",返回 "_8080"
     *
     * @example
     * storageKeySuffix(false); // 返回 "_zhongjyuan"
     *
     * @example
     * storageKeySuffix(false, "example"); // 返回 "_example"
     */
    storageKeySuffix: function (dynamic = true, suffix = zhongjyuan.config.storage.suffix) {
        let keySuffix;
        if (dynamic) {
            suffix = window.location.port;
        }
        // 根據(jù)suffix的值,確定使用不同的后綴函數(shù)
        if (suffix) {
            // 如果suffix存在,則定義一個名為keySuffix的函數(shù),返回帶下劃線前綴的suffix
            keySuffix = function () {
                return `_${suffix}`;
            };
        } else {
            // 如果suffix不存在,則定義一個名為keySuffix的函數(shù),返回空字符串
            keySuffix = function () {
                return "";
            };
        }
        // 返回實際的后綴字符串
        return keySuffix();
    },

storageKey:緩存的Key

/**
     * 緩存的Key
     * @author zhongjyuan
     * @email zhongjyuan@outlook.com
     * @website http://zhongjyuan.club
     * @param {*} key 鍵
     * @returns
     * @example
     * config.storageKeyType = "const";
     * config.storageKeySuffix = "example";
     *
     * let key1 = "data";
     * let mergedKey1 = storageKey(key1); // 調(diào)用storageKey函數(shù),在key后面添加后綴
     * console.log(mergedKey1); // 輸出:"data_example"
     *
     * config.storageKeyType = "dynamic";
     * window.location.port = "8080";
     *
     * let key2 = "info";
     * let mergedKey2 = storageKey(key2); // 調(diào)用storageKey函數(shù),不添加后綴
     * console.log(mergedKey2); // 輸出:"info_8080"
     */
    storageKey: function (key) {
        let mergeKey;
        const suffix = zhongjyuan.helper.storageKeySuffix(); // 獲取存儲鍵的后綴
        if (suffix) {
            mergeKey = function (key) {
                if (typeof key !== "string") {
                    throw new Error("key 必須是字符串!!"); // 如果key不是字符串,則拋出錯誤
                }
                return `${key}${suffix}`; // 返回將后綴添加到key后面的結(jié)果
            };
        } else {
            mergeKey = function (key) {
                if (typeof key !== "string") {
                    throw new Error("key 必須是字符串??!"); // 如果key不是字符串,則拋出錯誤
                }
                return key; // 返回原始的key值
            };
        }
        return mergeKey(key); // 調(diào)用合并函數(shù),返回最終的存儲鍵
    },

localStorage:localStorage操作對象

/**
     * @namespace localStorage
     * @description 提供操作瀏覽器 localStorage 的方法
     */
    localStorage: {
        /**
         * @method localStorage.set
         * @description 設置 localStorage 中的數(shù)據(jù)
         * @param {string} key 存儲的鍵名
         * @param {string|object} value 存儲的值(可以是字符串或?qū)ο螅?
         * @param {boolean} [manage=false] 是否增加管理(統(tǒng)一清除)
         * @returns {boolean} 是否設置成功
         * @example
         * localStorage.set('username', 'John');
         * localStorage.set('userInfo', { name: 'John', age: 25 }, true);
         */
        set: function (key, value, manage = false) {
            if (!zhongjyuan.helper.isString(key) || key === "" || zhongjyuan.helper.isNullOrUndefined(value)) {
                zhongjyuan.logger.warn("[helper] localStorage.set 參數(shù)錯誤:key不能為空或者value不能為undefined/null");
                return false;
            }
            if (typeof value === "object") {
                value = JSON.stringify(value);
            }
            try {
                window.localStorage.setItem(zhongjyuan.helper.storageKey(key), value);
                manage && zhongjyuan.storageManage["localStorage"].set(key);
            } catch (err) {
                console.log(`localStorage 存儲異常:${err}`);
                return false;
            }
            return true;
        },
        /**
         * @method localStorage.get
         * @description 獲取 localStorage 中存儲的數(shù)據(jù)
         * @param {string} key 要獲取的鍵名
         * @param {*} [defaultValue=null] 默認值(可選)
         * @returns {string|null} 存儲的值,如果不存在則返回默認值
         * @example
         * const username = localStorage.get('username');
         * const userInfo = localStorage.get('userInfo', null);
         */
        get: function (key, defaultValue) {
            defaultValue = arguments.length === 2 ? defaultValue : null;
            if (!zhongjyuan.helper.isString(key) || key === "") {
                arguments.length < 2 && zhongjyuan.logger.warn("[helper] localStorage.get 參數(shù)錯誤:key必須為字符串,且不能為空");
                return defaultValue;
            }
            const val = window.localStorage.getItem(zhongjyuan.helper.storageKey(key));
            if (!zhongjyuan.helper.isNull(val)) {
                return val;
            }
            return defaultValue;
        },
        /**
         * @method localStorage.remove
         * @description 移除 localStorage 中指定的數(shù)據(jù)
         * @param {string} key 要移除的鍵名
         * @returns {boolean} 是否刪除成功
         * @example
         * localStorage.remove('username');
         */
        remove: function (key) {
            if (!zhongjyuan.helper.isString(key) || key === "") {
                zhongjyuan.logger.warn("[helper] localStorage.remove 參數(shù)錯誤:key不能為空");
                return false;
            }
            window.localStorage.removeItem(zhongjyuan.helper.storageKey(key));
            return true;
        },
        /**
         * @method localStorage.clear
         * @description 清空整個 localStorage 中的所有數(shù)據(jù)
         * @example
         * localStorage.clear();
         */
        clear: function () {
            window.localStorage.clear();
        },
    },

sessionStorage:sessionStorage操作對象

/**
     * @namespace sessionStorage
     * @description 提供操作瀏覽器 sessionStorage 的方法
     */
    sessionStorage: {
        /**
         * 存儲數(shù)據(jù)到sessionStorage中
         *
         * @method sessionStorage.set
         * @param {string} key 鍵名
         * @param {string|object} value 值
         * @param {boolean} [manage=false] 是否增加管理(統(tǒng)一清除)
         * @return {boolean} 表示是否設置成功
         *
         * @example
         * sessionStorage.set('username', 'John');
         * sessionStorage.set('userInfo', { name: 'John', age: 28 }, true);
         */
        set: function (key, value, manage = false) {
            if (!zhongjyuan.helper.isString(key) || key === "" || zhongjyuan.helper.isNullOrUndefined(value)) {
                zhongjyuan.logger.warn("[helper] sessionStorage.set 參數(shù)錯誤:key不能為空或者value不能為undefined/null");
                return false;
            }
            if (typeof value === "object") {
                value = JSON.stringify(value);
            }
            try {
                window.sessionStorage.setItem(zhongjyuan.helper.storageKey(key), value);
                manage && zhongjyuan.storageManage["sessionStorage"].set(key);
            } catch (err) {
                console.log(`sessionStorage 存儲異常:${err}`);
                return false;
            }
            return true;
        },
        /**
         * 從sessionStorage中獲取指定鍵的值
         *
         * @method sessionStorage.get
         * @param {string} key 鍵名
         * @param {*} [defaultValue=null] 默認值(可選)
         * @return {string|null} 鍵對應的值,如果鍵不存在則返回默認值或null
         *
         * @example
         * const username = sessionStorage.get('username');
         * const userInfo = sessionStorage.get('userInfo', { name: 'Guest' });
         */
        get: function (key, defaultValue) {
            defaultValue = arguments.length === 2 ? defaultValue : null;
            if (!zhongjyuan.helper.isString(key) || key === "") {
                arguments.length < 2 && zhongjyuan.logger.warn("[helper] sessionStorage.get 參數(shù)錯誤:key必須為字符串,且不能為空");
                return defaultValue;
            }
            const val = window.sessionStorage.getItem(zhongjyuan.helper.storageKey(key));
            if (!zhongjyuan.helper.isNull(val)) {
                return val;
            }
            return defaultValue;
        },
        /**
         * 從sessionStorage中移除指定的鍵值對
         *
         * @method sessionStorage.remove
         * @param {string} key 鍵名
         * @return {boolean} 是否刪除成功
         *
         * @example
         * sessionStorage.remove('username');
         */
        remove: function (key) {
            if (!zhongjyuan.helper.isString(key) || key === "") {
                zhongjyuan.logger.warn("[helper] sessionStorage.remove 參數(shù)錯誤:key不能為空");
                return false;
            }
            window.sessionStorage.removeItem(zhongjyuan.helper.storageKey(key));
            return true;
        },
        /**
         * 清空sessionStorage中所有的鍵值對
         *
         * @method sessionStorage.clear
         *
         * @example
         * sessionStorage.clear();
         */
        clear: function () {
            window.sessionStorage.clear();
        },
    },

cookie:cookie操作對象

/**
     * @namespace cookie
     * @description 提供操作瀏覽器 cookie 的方法
     */
    cookie: {
        /**
         * @method cookie.set
         * @desc 設置Cookie
         * @param {string} key 鍵名
         * @param {string|number|boolean} value 值
         * @param {number} expiredays 過期時間(天數(shù))
         * @param {boolean} [noStoreKey=false] 是否不設置存儲鍵(可選,默認為false)
         * @returns {boolean} 表示是否設置成功
         * @example
         * // 設置名為"username"的Cookie,值為"zhongjyuan",過期時間為7天
         * var success = helper.cookie.set('username', 'zhongjyuan', 7);
         */
        set: function (key, value, expiredays, noStoreKey) {
            var date;
            if (!zhongjyuan.helper.isString(key) || key === "" || zhongjyuan.helper.isNullOrUndefined(value)) {
                zhongjyuan.logger.warn("[helper] cookie.set 參數(shù)錯誤:key不能為空或者value不能為undefined/null");
                return false;
            }
            if (zhongjyuan.helper.isInt(expiredays)) {
                date = new Date();
                date.setDate(date.getDate() + expiredays);
            }
            document.cookie =
                (noStoreKey ? key : zhongjyuan.helper.storageKey(key)) +
                "=" +
                escape(value) +
                (!date ? "" : ";expires=" + date.toGMTString()) +
                (zhongjyuan.config.cookie.useMainDomain ? ";domain=" + zhongjyuan.helper.getDomain() : "") +
                ";path=/;SameSite=" +
                zhongjyuan.config.cookie.sameSite +
                (zhongjyuan.config.cookie.secure ? ";Secure" : "");
            return true;
        },
        /**
         * @method cookie.get
         * @desc 獲取Cookie的值
         * @param {string} key 鍵名
         * @param {*} [defaultValue=null] 默認值(可選,默認為null)
         * @param {boolean} [noStoreKey=false] 否不檢索存儲鍵(可選,默認為false)
         * @returns {string|null} Cookie的值,如果未找到則返回默認值
         * @example
         * // 獲取名為"username"的Cookie的值,如果不存在則返回"default"
         * var username = helper.cookie.get('username', 'default');
         */
        get: function (key, defaultValue, noStoreKey) {
            defaultValue = arguments.length === 2 ? defaultValue : null;
            if (!zhongjyuan.helper.isString(key) || key === "") {
                zhongjyuan.logger.warn("[helper] cookie.get 參數(shù)錯誤:key不能為空");
                return defaultValue;
            }
            var arr = document.cookie.match(new RegExp("(^| )" + (noStoreKey ? key : zhongjyuan.helper.storageKey(key)) + "=([^;]*)(;|$)"));
            if (zhongjyuan.helper.isArray(arr)) {
                return unescape(arr[2]);
            }
            return defaultValue;
        },
        /**
         * @method cookie.remove
         * @desc 移除Cookie
         * @param {string} key 鍵名
         * @param {boolean} [noStoreKey] 是否不移除存儲的鍵,默認為false
         * @returns {boolean} 是否成功移除了Cookie
         * @example
         * helper.cookie.remove('username');
         */
        remove: function (key, noStoreKey) {
            return zhongjyuan.helper.cookie.set(key, "", -1000, noStoreKey);
        },
    },

以上就是緩存處理函數(shù)storageKeySuffix示例解析的詳細內(nèi)容,更多關于緩存處理函數(shù)的資料請關注腳本之家其它相關文章!

相關文章

最新評論

稻城县| 潮安县| 桦甸市| 石首市| 东安县| 怀化市| 治县。| 南召县| 河源市| 常宁市| 莆田市| 利辛县| 三明市| 河北省| 华坪县| 博爱县| 磴口县| 弥勒县| 普宁市| 化德县| 定安县| 正蓝旗| 夹江县| 伊金霍洛旗| 平邑县| 大埔区| 云南省| 阳高县| 安化县| 两当县| 宁远县| 玛曲县| 辽中县| 景德镇市| 鄂伦春自治旗| 舒城县| 田林县| 永平县| 高邑县| 榆树市| 锡林郭勒盟|