-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache-min.js
2 lines (2 loc) · 2.8 KB
/
cache-min.js
1
2
var Cache=function(){"use strict";function t(t){this.options=t=t||{},this.expireHash={},this.keys=[],"object"==typeof t.storage?(this.webStorage=!0,this.storage=t.storage,this.keyPrefix=(t.keyPrefix||"cache"+l.length)+":",e.call(this)):(this.storage={},this.keyPrefix=""),this.length=this.keys.length,this.setLimit(t.limit),l.push(this)}function e(){for(var t,e,s,n,r=this.storage,h=this.keyPrefix,o=h.length,l=Number.POSITIVE_INFINITY,f={},u=0,g=0,c=r.length;c>g;++g)n=r.key(g),0===n.indexOf(h)&&(t=r[n],n=n.slice(o),e=t.substring(t.indexOf("_")+1,t.indexOf(":")),i.call(this,n,parseInt(e,10))&&(s=parseInt(t,10),f[s]=n,++u,l>s&&(l=s)));f.length=u?u+l:0,this.index=f.length-1,this.keys=a.call(f,l)}function i(t,e){if("undefined"==typeof e)return!0;var i=new Date;if(i>=e)return!1;var s=this.expireHash,n=this;return e in s?s[e].push(t):(s[e]=[t],setTimeout(function(){r.apply(n,s[e]),delete s[e]},Math.max(0,e-i))),!0}function s(t,e){if(t){var i;if(t.maxAge)return i=new Date((new Date).getTime()+t.maxAge),isNaN(i)?void 0:+i;if(t.expires)return i=t.expires,isNaN(i)?void 0:+i}return e?s(e):void 0}function n(){var t=this.length-this.limit;t>0&&(h.call(this,this.keys.splice(0,t)),this.length=this.keys.length)}function r(){for(var t,e,i=this.keys,s=this.keyPrefix,n=this.storage,r=0,h=arguments.length;h>r;++r)e=arguments[r],t=o(i,""+e),t>=0&&(i.splice(t,1),delete n[s+e]);this.length=i.length}function h(t){for(var e,i=this.storage,s=this.keyPrefix,n=this.webStorage,r=0,h=t.length;h>r;++r)e=s+t[r],(!n||i[e])&&delete i[e]}var a=[].slice,o="function"==typeof[].indexOf?function(t,e){return t.indexOf(e)}:function(t,e){for(var i=0,s=t.length;s>i;++i)if(t[i]===e)return i;return-1},l=[],f=t.prototype;return f.hasKey=function(t){return o(this.keys,""+t)>=0},f.setItem=function(t,e,r){var h=s(r,this.options),a=this.keys;return t=""+t,this.removeItem(t),i.call(this,t,h)&&(this.webStorage&&("undefined"==typeof h&&(h=""),e=++this.index+"_"+h+":"+e),this.storage[this.keyPrefix+t]=e,a.push(t),this.length=a.length,n.call(this)),this},f.getItem=function(t,e){if(arguments.length<2||this.hasKey(t)){var i=this.storage[this.keyPrefix+t];return this.webStorage?i?i.substring(i.indexOf(":")+1):void 0:i}return e},f.removeItem=function(t){t=""+t;var e=this.keys,i=o(e,""+t);if(i>=0){var s,n=this.expireHash;for(var r in n)if(s=n[r],i=o(s,t),i>=0){s.splice(i,1);break}e.splice(i,1),this.length=e.length,delete this.storage[this.keyPrefix+t]}return this},f.setLimit=function(t){return t=parseInt(t,10),t>=0?(this.limit=t,n.call(this)):delete this.limit,this},f.clear=function(){return this.webStorage?h.call(this,this.keys):this.storage={},this.keys=[],this.length=0,this},f.key=function(t){return this.keys[t]},t.clear=function(){for(var t=0,e=l.length;e>t;++t)l[t].clear()},"undefined"!=typeof module&&module.exports&&(module.exports=t),t}();
//# sourceMappingURL=cache-min.map