緩存處理函數(shù)storageKeySuffix操作示例解析
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)文章
C語(yǔ)言實(shí)現(xiàn)數(shù)組移位、前移、后移與整體移動(dòng)實(shí)例代碼
C語(yǔ)言中通??梢允褂醚h(huán)語(yǔ)句實(shí)現(xiàn)數(shù)組的移動(dòng),下面這篇文章主要給大家介紹了關(guān)于C語(yǔ)言實(shí)現(xiàn)數(shù)組移位、前移、后移與整體移動(dòng)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-03-03詳解C++中的函數(shù)調(diào)用和下標(biāo)以及成員訪問(wèn)運(yùn)算符的重載
這篇文章主要介紹了詳解C++中的函數(shù)調(diào)用和下標(biāo)以及成員訪問(wèn)運(yùn)算符,講到了這些二元運(yùn)算符使用的語(yǔ)法及重載,需要的朋友可以參考下2016-01-01C++服務(wù)器和客戶端交互的項(xiàng)目實(shí)踐
本文主要介紹了C++服務(wù)器和客戶端交互的項(xiàng)目實(shí)踐,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07OpenCV實(shí)現(xiàn)圖像去噪算法的步驟詳解
這篇文章主要為大家介紹了OpenCV中圖像去噪算法的原理,文中通過(guò)示例為大家詳細(xì)講解了圖像去噪算法的使用,感興趣的小伙伴可以了解一下2022-06-06C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單的通訊錄管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)通訊錄管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06VC程序在Win32環(huán)境下動(dòng)態(tài)鏈接庫(kù)(DLL)編程原理
這篇文章主要介紹了VC程序在Win32環(huán)境下動(dòng)態(tài)鏈接庫(kù)(DLL)編程原理,包括了dll文件的原理與具體實(shí)現(xiàn)過(guò)程,對(duì)于深入掌握VC程序設(shè)計(jì)具有很好的參考借鑒價(jià)值,需要的朋友可以參考下2014-10-10總結(jié)C語(yǔ)言中const關(guān)鍵字的使用
一起雖然學(xué)過(guò)c語(yǔ)言,但是并沒(méi)有寫過(guò)太多的代碼,最近想要拾起c語(yǔ)言,就寫了一些代碼,但是對(duì)const關(guān)鍵字比較陌生,這里總結(jié)一下,方法自己和大家有需要的時(shí)候參考借鑒,下面跟著小編一起學(xué)習(xí)學(xué)習(xí)吧。2016-11-11在C語(yǔ)言中轉(zhuǎn)換時(shí)間的基本方法介紹
這篇文章主要介紹了在C語(yǔ)言中轉(zhuǎn)換時(shí)間的基本方法,分別是mktime()函數(shù)和localtime()函數(shù)的使用,需要的朋友可以參考下2015-08-08