欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

storageKeySuffix:生成存儲(chǔ)鍵的后綴字符串

/**
     * 生成存儲(chǔ)鍵的后綴字符串
     * @author zhongjyuan
     * @email zhongjyuan@outlook.com
     * @website http://zhongjyuan.club
     * @param {boolean} dynamic 是否使用動(dòng)態(tài)后綴
     * @param {string} suffix 靜態(tài)后綴值,默認(rèn)為 "zhongjyuan"
     * @returns {string} 存儲(chǔ)鍵的后綴字符串
     *
     * @example
     * storageKeySuffix(); // 假設(shè)當(dāng)前窗口的端口號(hào)為 "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存在,則定義一個(gè)名為keySuffix的函數(shù),返回帶下劃線前綴的suffix
            keySuffix = function () {
                return `_${suffix}`;
            };
        } else {
            // 如果suffix不存在,則定義一個(gè)名為keySuffix的函數(shù),返回空字符串
            keySuffix = function () {
                return "";
            };
        }
        // 返回實(shí)際的后綴字符串
        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(); // 獲取存儲(chǔ)鍵的后綴
        if (suffix) {
            mergeKey = function (key) {
                if (typeof key !== "string") {
                    throw new Error("key 必須是字符串?。?); // 如果key不是字符串,則拋出錯(cuò)誤
                }
                return `${key}${suffix}`; // 返回將后綴添加到key后面的結(jié)果
            };
        } else {
            mergeKey = function (key) {
                if (typeof key !== "string") {
                    throw new Error("key 必須是字符串!!"); // 如果key不是字符串,則拋出錯(cuò)誤
                }
                return key; // 返回原始的key值
            };
        }
        return mergeKey(key); // 調(diào)用合并函數(shù),返回最終的存儲(chǔ)鍵
    },

localStorage:localStorage操作對(duì)象

/**
     * @namespace localStorage
     * @description 提供操作瀏覽器 localStorage 的方法
     */
    localStorage: {
        /**
         * @method localStorage.set
         * @description 設(shè)置 localStorage 中的數(shù)據(jù)
         * @param {string} key 存儲(chǔ)的鍵名
         * @param {string|object} value 存儲(chǔ)的值(可以是字符串或?qū)ο螅?
         * @param {boolean} [manage=false] 是否增加管理(統(tǒng)一清除)
         * @returns {boolean} 是否設(shè)置成功
         * @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ù)錯(cuò)誤: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 存儲(chǔ)異常:${err}`);
                return false;
            }
            return true;
        },
        /**
         * @method localStorage.get
         * @description 獲取 localStorage 中存儲(chǔ)的數(shù)據(jù)
         * @param {string} key 要獲取的鍵名
         * @param {*} [defaultValue=null] 默認(rèn)值(可選)
         * @returns {string|null} 存儲(chǔ)的值,如果不存在則返回默認(rèn)值
         * @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ù)錯(cuò)誤: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ù)錯(cuò)誤:key不能為空");
                return false;
            }
            window.localStorage.removeItem(zhongjyuan.helper.storageKey(key));
            return true;
        },
        /**
         * @method localStorage.clear
         * @description 清空整個(gè) localStorage 中的所有數(shù)據(jù)
         * @example
         * localStorage.clear();
         */
        clear: function () {
            window.localStorage.clear();
        },
    },

sessionStorage:sessionStorage操作對(duì)象

/**
     * @namespace sessionStorage
     * @description 提供操作瀏覽器 sessionStorage 的方法
     */
    sessionStorage: {
        /**
         * 存儲(chǔ)數(shù)據(jù)到sessionStorage中
         *
         * @method sessionStorage.set
         * @param {string} key 鍵名
         * @param {string|object} value 值
         * @param {boolean} [manage=false] 是否增加管理(統(tǒng)一清除)
         * @return {boolean} 表示是否設(shè)置成功
         *
         * @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ù)錯(cuò)誤: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 存儲(chǔ)異常:${err}`);
                return false;
            }
            return true;
        },
        /**
         * 從sessionStorage中獲取指定鍵的值
         *
         * @method sessionStorage.get
         * @param {string} key 鍵名
         * @param {*} [defaultValue=null] 默認(rèn)值(可選)
         * @return {string|null} 鍵對(duì)應(yīng)的值,如果鍵不存在則返回默認(rèn)值或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ù)錯(cuò)誤:key必須為字符串,且不能為空");
                return defaultValue;
            }
            const val = window.sessionStorage.getItem(zhongjyuan.helper.storageKey(key));
            if (!zhongjyuan.helper.isNull(val)) {
                return val;
            }
            return defaultValue;
        },
        /**
         * 從sessionStorage中移除指定的鍵值對(duì)
         *
         * @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ù)錯(cuò)誤:key不能為空");
                return false;
            }
            window.sessionStorage.removeItem(zhongjyuan.helper.storageKey(key));
            return true;
        },
        /**
         * 清空sessionStorage中所有的鍵值對(duì)
         *
         * @method sessionStorage.clear
         *
         * @example
         * sessionStorage.clear();
         */
        clear: function () {
            window.sessionStorage.clear();
        },
    },

cookie:cookie操作對(duì)象

/**
     * @namespace cookie
     * @description 提供操作瀏覽器 cookie 的方法
     */
    cookie: {
        /**
         * @method cookie.set
         * @desc 設(shè)置Cookie
         * @param {string} key 鍵名
         * @param {string|number|boolean} value 值
         * @param {number} expiredays 過(guò)期時(shí)間(天數(shù))
         * @param {boolean} [noStoreKey=false] 是否不設(shè)置存儲(chǔ)鍵(可選,默認(rèn)為false)
         * @returns {boolean} 表示是否設(shè)置成功
         * @example
         * // 設(shè)置名為"username"的Cookie,值為"zhongjyuan",過(guò)期時(shí)間為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ù)錯(cuò)誤: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] 默認(rèn)值(可選,默認(rèn)為null)
         * @param {boolean} [noStoreKey=false] 否不檢索存儲(chǔ)鍵(可選,默認(rèn)為false)
         * @returns {string|null} Cookie的值,如果未找到則返回默認(rèn)值
         * @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ù)錯(cuò)誤: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] 是否不移除存儲(chǔ)的鍵,默認(rèn)為false
         * @returns {boolean} 是否成功移除了Cookie
         * @example
         * helper.cookie.remove('username');
         */
        remove: function (key, noStoreKey) {
            return zhongjyuan.helper.cookie.set(key, "", -1000, noStoreKey);
        },
    },

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

相關(guān)文章

最新評(píng)論