iweb-cache
Version:
轻量且强悍的 Cache库, 为您暴露丰富而统一的 API, 使得业务调用更加简便.
2 lines (1 loc) • 3.35 kB
JavaScript
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 a=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 i=function(){function r(e){var n,a,i;t(this,r),i={},(a="appData")in(n=this)?Object.defineProperty(n,a,{value:i,enumerable:!0,configurable:!0,writable:!0}):n[a]=i,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 a,i=(a=r,Object.prototype.toString.call(a).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"===i&&(r="undefined"),"object"!==i&&"array"!==i&&"null"!==i&&"boolean"!==i&&"number"!==i||(r=JSON.stringify(r));var o={value:r,valueType:i,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 a=r.reduce((function(t,r){var n=e.get(r);return t.push(n),t}),[]);return a}},{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};function s(e){var t=Object.assign({},o,e),r=new a(t);return new i(r)}export default s;