UNPKG

cache2

Version:

一个简单的 JavaScript 缓存管理,支持浏览器端和 node 端。

3 lines (2 loc) 7.39 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).cache2={})}(this,(function(t){"use strict";var e=function(t,r){return e=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r])},e(t,r)};var r=function(){return r=Object.assign||function(t){for(var e,r=1,o=arguments.length;r<o;r++)for(var n in e=arguments[r])Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n]);return t},r.apply(this,arguments)};"function"==typeof SuppressedError&&SuppressedError;var o=function(){function t(){this.handlers={}}return t.prototype.eventNames=function(){var t,e=(null===(t=Object.getOwnPropertySymbols)||void 0===t?void 0:t.call(Object,this.handlers))||[];return Object.keys(this.handlers).concat(e)},t.prototype.rawListeners=function(t){var e=this.handlers[t];return e?e.map((function(t){return t.raw})):[]},t.prototype.listeners=function(t){var e=this.handlers[t];return e?e.map((function(t){return t.wrap})):[]},t.prototype.hasListener=function(t,e){return this.rawListeners(t).some((function(t){return t===e}))},t.prototype._on=function(t,e,r,o,n){void 0===o&&(o=null),void 0===n&&(n=1);var i={raw:e,wrap:r,context:o};if(this.handlers[t]){var s=1===n?"push":"unshift";this.handlers[t][s](i)}else this.handlers[t]=[i];return this},t.prototype.prependListener=function(t,e,r){return this._on(t,e,e,r,0)},t.prototype.on=function(t,e,r){return this._on(t,e,e,r)},t.prototype._wrapOnce=function(t,e,r){var o=this;void 0===r&&(r=null);var n=function(){for(var i=[],s=0;s<arguments.length;s++)i[s]=arguments[s];e.apply(r,i),o.off(t,n)};return n},t.prototype.once=function(t,e,r){var o=this._wrapOnce(t,e,r);return this._on(t,e,o,r)},t.prototype.prependOnceListener=function(t,e,r){var o=this._wrapOnce(t,e,r);return this._on(t,e,o,r,0)},t.prototype.off=function(t,e){var r=this.handlers[t];if(r)if(e){var o=r.findIndex((function(t){return t.wrap===e||t.raw===e}));-1!==o&&r.splice(o,1)}else delete this.handlers[t];return this},t.prototype.offAll=function(){return this.handlers={},this},t.prototype.emit=function(t){for(var e=[],r=1;r<arguments.length;r++)e[r-1]=arguments[r];var o=this.handlers[t];return!!(o&&o.length>0)&&(o.forEach((function(t){t.wrap.apply(t.context,e)})),!0)},t}(),n={},i=function(){function t(t){void 0===t&&(t="default"),this.scope=t,n[this.scope]||(n[this.scope]={}),this.data=n[this.scope]}return t.prototype.getItem=function(t){return t in this.data?this.data[t]:null},t.prototype.setItem=function(t,e){this.data[t]=e},t.prototype.removeItem=function(t){delete this.data[t]},t.prototype.clear=function(){n[this.scope]={},this.data=n[this.scope]},t}();function s(t){try{var e="object"==typeof t&&null!==t&&!!t.setItem&&!!t.getItem&&!!t.removeItem;if(e){var r=Math.random().toString(16).substring(2,8)+(new Date).getTime();if(t.setItem(r,"1"),"1"!==t.getItem(r))return!1;t.removeItem(r)}return e}catch(e){return console.error("[cache2] ".concat(t," is not supported. The default memory cache will be used.")),!1}}var c=function(){function t(t,e){void 0===e&&(e={});var o=!!t&&s(t);this.options=r({needParsed:o,prefix:""},e),this.storage=o?t:new i(this.options.memoryScope)}return t.prototype.getKey=function(t){return this.options.prefix+t},t.prototype.get=function(t){var e=this.storage.getItem(this.getKey(t));return this.options.needParsed?function(t,e){try{return JSON.parse(t,e)}catch(e){return t}}(e,this.options.reviver):e},t.prototype.set=function(t,e){this.storage.setItem(this.getKey(t),this.options.needParsed?function(t,e){return JSON.stringify(t,e)}(e,this.options.replacer):e)},t.prototype.del=function(t){this.storage.removeItem(this.getKey(t))},t.prototype.clear=function(){"function"==typeof this.storage.clear&&this.storage.clear()},t}(),a="default",p=function(t){function o(e,o){var n,i=t.call(this)||this,s=a;return"string"==typeof e?s=e||a:"object"==typeof e&&(n=e),n||"object"!=typeof o||(n=o),i.options=r({max:-1,stdTTL:0,maxStrategy:"limited",checkperiod:0,prefix:"cache2_"},n),i.storage=new c(i.options.storage,r({memoryScope:s},i.options)),i.cacheKey=s,i.startCheckperiod(),i}return function(t,r){if("function"!=typeof r&&null!==r)throw new TypeError("Class extends value "+String(r)+" is not a constructor or null");function o(){this.constructor=t}e(t,r),t.prototype=null===r?Object.create(r):(o.prototype=r.prototype,new o)}(o,t),o.prototype._check=function(t,e){var r=!0;return 0!==e.t&&e.t<Date.now()&&(r=!1,this.del(t),this.emit("expired",t,e.v)),r},o.prototype._wrap=function(t,e){var r=Date.now(),o="number"==typeof e?e:this.options.stdTTL;return{v:t,t:o>0?r+o:0,n:r}},o.prototype._isLimited=function(t){return this.options.max>-1&&t>=this.options.max},o.prototype._getReplaceKey=function(t,e){var r=t[0];return t.forEach((function(t){(e[t].t<e[r].t||e[t].t===e[r].t&&e[t].n<e[r].n)&&(r=t)})),r},Object.defineProperty(o.prototype,"cacheValues",{get:function(){return this.storage.get(this.cacheKey)||{}},enumerable:!1,configurable:!0}),o.prototype.setCacheValues=function(t){this.storage.set(this.cacheKey,t)},o.prototype.get=function(t){var e=this.cacheValues[t];if(e&&this._check(t,e))return e.v},o.prototype.mget=function(t){var e=this,r={};if(!Array.isArray(t))return r;var o=this.cacheValues;return t.forEach((function(t){var n=o[t];n&&e._check(t,n)&&(r[t]=n.v)})),r},o.prototype.getAll=function(){var t=Object.keys(this.cacheValues);return this.mget(t)},o.prototype.set=function(t,e,r){if(0===this.options.max)return!1;var o=this.cacheValues,n=Object.keys(o);if(!o[t]&&this._isLimited(n.length)){var i=this.keys();if(this._isLimited(i.length)){if("replaced"!==this.options.maxStrategy)return!1;var s=this._getReplaceKey(i,o);this.del(s)}}return o[t]=this._wrap(e,r),this.setCacheValues(o),this.emit("set",t,o[t].v),!0},o.prototype.mset=function(t){var e=this,r=!0;return t.forEach((function(t){var o=e.set(t.key,t.value,t.ttl);r&&!o&&(r=!1)})),r},o.prototype.del=function(t){var e=this,r=this.cacheValues,o=0;return(Array.isArray(t)?t:[t]).forEach((function(t){if(r[t]){o++;var n=r[t];delete r[t],e.emit("del",t,n.v)}})),o>0&&this.setCacheValues(r),o},o.prototype.clear=function(){this.storage.del(this.cacheKey)},o.prototype.keys=function(){var t=this,e=this.cacheValues;return Object.keys(e).filter((function(r){return t._check(r,e[r])}))},o.prototype.has=function(t){var e=this.cacheValues[t];return!(!e||!this._check(t,e))},o.prototype.take=function(t){var e,r=this.cacheValues[t];return r&&this._check(t,r)&&(e=r.v,this.del(t)),e},o.prototype.ttl=function(t,e){var r=this.cacheValues,o=r[t];return!(!o||!this._check(t,o))&&(r[t]=this._wrap(o.v,e),!0)},o.prototype.getTtl=function(t){var e=this.cacheValues,r=e[t];if(r&&this._check(t,r))return e[t].t},o.prototype.getLastModified=function(t){var e=this.cacheValues,r=e[t];if(r&&this._check(t,r))return e[t].n},o.prototype.startCheckperiod=function(){var t=this;this.keys(),this.options.checkperiod>0&&(clearTimeout(this._checkTimeout),this._checkTimeout=setTimeout((function(){t.startCheckperiod()}),this.options.checkperiod))},o.prototype.stopCheckperiod=function(){clearTimeout(this._checkTimeout)},o}(o);t.Cache=p,t.Storage=c,t.default=p,Object.defineProperty(t,"__esModule",{value:!0})})); //# sourceMappingURL=cache2.min.js.map