iweb-cache
Version:
轻量且强悍的 Cache库, 为您暴露丰富而统一的 API, 使得业务调用更加简便.
2 lines (1 loc) • 3.58 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).iwebCache=t()}(this,(function(){"use strict";function e(t){return(e="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(t)}function t(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function r(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}function n(e,t,n){return t&&r(e.prototype,t),n&&r(e,n),e}var i=function(){function e(r){t(this,e),this.options=r,this.storage=window[this.options.storageType]}return n(e,[{key:"del",value:function(e){this.storage.removeItem(e)}},{key:"clear",value:function(){this.storage.clear()}},{key:"get",value:function(e){return this.storage.getItem(e)}},{key:"save",value:function(e){this.storage.setItem(this.options.prefix,JSON.stringify(e))}},{key:"getCacheData",value:function(){var e=this.storage.getItem(this.options.prefix);try{return e?JSON.parse(e):{}}catch(e){return console.error(e),{}}}}]),e}();var a=function(){function r(e){var n,i,a;t(this,r),a={},(i="appData")in(n=this)?Object.defineProperty(n,i,{value:a,enumerable:!0,configurable:!0,writable:!0}):n[i]=a,this.clientStore=e,this.appData=this.clientStore.getCacheData()}return n(r,[{key:"save",value:function(t,r,n){if(!t||"string"!=typeof t)throw new Error("key not string!");var i,a=(i=r,Object.prototype.toString.call(i).substring(8).replace(/]/,"").toLocaleLowerCase());if("string"==typeof r&&(r=r.trim()),!function(t){return!(t instanceof Function||"symbol"===e(t)||"bigint"==typeof t)}(r))throw new Error("Check whether the stored value can be serialized by JSON.stringify()");"undefined"===a&&(r="undefined"),"object"!==a&&"array"!==a&&"null"!==a&&"boolean"!==a&&"number"!==a||(r=JSON.stringify(r));var o={value:r,valueType:a,expires:this.getExpires(n)};return this.appData[t]=o,this.clientStore.save(this.appData),!0}},{key:"get",value:function(e){var t=!!this.appData[e]&&this.appData[e],r=+new Date;if(t&&t.expires&&r>Number(t.expires)||!t)return delete this.appData[e],this.clientStore.save(this.appData),!1;var n=t.valueType;if("undefined"!==n){if("string"===n||"regexp"===n)return t.value;try{return JSON.parse(t.value)}catch(e){return!1}}}},{key:"getAll",value:function(){for(var e=this,t=arguments.length,r=new Array(t),n=0;n<t;n++)r[n]=arguments[n];r.length;var i=r.reduce((function(t,r){var n=e.get(r);return t.push(n),t}),[]);return i}},{key:"del",value:function(e){return!!this.appData[e]&&(delete this.appData[e],this.clientStore.del(e),!0)}},{key:"clear",value:function(){return this.appData={},this.clientStore.clear(),!0}},{key:"cover",value:function(e,t){var r=this.appData[e];return r&&r.expires?this.save(e,t,r.expires-+new Date):this.save(e,t),!0}},{key:"getExpires",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:0;if(e||(e=this.clientStore.options.expires),"number"!=typeof e)throw new Error("expires not number!");return e?+new Date+e:0}},{key:"getCacheSize",value:function(){var e=0;if(this.appData){for(var t in this.appData)e+=JSON.stringify(this.appData[t]).length;return e}return!1}}]),r}(),o={storageType:"localStorage",prefix:"iweb-cache",expires:0};return function(e){var t=Object.assign({},o,e),r=new i(t);return new a(r)}}));