UNPKG

timed-cache

Version:

A minimalist time-based caching system.

1 lines 1.13 kB
!function(t,e){var n,o,i;"function"==typeof define&&define.amd?define(e):"undefined"!=typeof module&&module.exports?module.exports=e():(n=this,o=e(),i=n[t],o.noConflict=function(){return n[t]=i,o},n[t]=o)}("Cache",function(){function l(t){return"string"!=typeof t?this.prefix+JSON.stringify(t):this.prefix+t}function t(t){this.defaultTtl=t&&t.defaultTtl?t.defaultTtl:6e4,this.prefix="__cache__"}var u={};return t.prototype.put=function(t,e,n){var o=(n?n.ttl:void 0)||this.defaultTtl,i=(n?n.callback:void 0)||function(){},r=l.call(this,t),a=this,n=u[r];n&&clearTimeout(n.handle);o=setTimeout(function(){return a.remove(t)},o);u[r]={handle:o,data:e,callback:i}},t.prototype.get=function(t,e,n){var o=u[l.call(this,t)];if(o||"function"!=typeof e)return o&&o.data;e=e.apply(this);return this.put(t,e,n),e},t.prototype.remove=function(t){var e=l.call(this,t),n=u[e];n&&(clearTimeout(n.handle),delete u[e],n.callback(t,n.data))},t.prototype.clear=function(){for(var t in u)n=t,null!==(e=u)&&Object.prototype.hasOwnProperty.call(e,n)&&clearTimeout(u[t].handle);var e,n;u={}},t.prototype.size=function(){return Object.keys(u).length},t});