smart-cache-gc
Version:
A GC-aware cache using WeakRef and FinalizationRegistry
2 lines (1 loc) • 6.97 kB
JavaScript
"use strict";var A=Object.defineProperty;var P=Object.getOwnPropertyDescriptor;var Q=Object.getOwnPropertyNames;var X=Object.prototype.hasOwnProperty;var F=u=>{throw TypeError(u)};var Y=(u,e)=>{for(var t in e)A(u,t,{get:e[t],enumerable:!0})},Z=(u,e,t,s)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of Q(e))!X.call(u,r)&&r!==t&&A(u,r,{get:()=>e[r],enumerable:!(s=P(e,r))||s.enumerable});return u};var _=u=>Z(A({},"__esModule",{value:!0}),u);var C=(u,e,t)=>e.has(u)||F("Cannot "+t);var i=(u,e,t)=>(C(u,e,"read from private field"),t?t.call(u):e.get(u)),m=(u,e,t)=>e.has(u)?F("Cannot add the same private member more than once"):e instanceof WeakSet?e.add(u):e.set(u,t),E=(u,e,t,s)=>(C(u,e,"write to private field"),s?s.call(u,t):e.set(u,t),t),o=(u,e,t)=>(C(u,e,"access private method"),t);var w=(u,e,t,s)=>({set _(r){E(u,e,r,t)},get _(){return i(u,e,s)}});var $={};Y($,{SmartCache:()=>z});module.exports=_($);var h,a,c,I,v,T,l,g,p,b,n,L,U,k,M,D,J,O,V,W,j,y,G,H,q,z=class{constructor(e){m(this,n);m(this,h,new Map);m(this,a,new Map);m(this,c,new Map);m(this,I);m(this,v);m(this,T,{key:null,prev:null,next:null});m(this,l,{key:null,prev:null,next:null});m(this,g,new Map);m(this,p,0);m(this,b,(()=>{if(typeof global.FinalizationRegistry!="function")throw new Error("FinalizationRegistry is not supported in this environment");return new global.FinalizationRegistry(e=>{o(this,n,V).call(this,e)})})());if(typeof global.FinalizationRegistry!="function")throw new Error("FinalizationRegistry is not supported in this environment");E(this,I,e?.defaultTtl),E(this,v,e?.maxSize),i(this,T).next=i(this,l),i(this,l).prev=i(this,T)}get(e){let{entry:t,keyType:s}=o(this,n,L).call(this,e);if(t){if(t.expiresAt&&o(this,n,y).call(this,t.expiresAt)){this.delete(e);return}switch(o(this,n,U).call(this,e),s){case"string":case"symbol":return t.value;case"object":{let d=t.weakRef.deref();if(!d){this.delete(e);return}return d}}}}set(e,t,s){o(this,n,O).call(this,e,t);let{entry:r,keyType:d}=o(this,n,L).call(this,e);r&&(o(this,n,W).call(this,e),o(this,n,U).call(this,e)),i(this,v)&&i(this,p)>=i(this,v)&&o(this,n,J).call(this);let f=s?.ttl??i(this,I),S=f?Date.now()+f:void 0,N=Date.now();if(d==="string"){let K=f?o(this,n,j).call(this,e,f):void 0,x={value:t,timeoutId:K,expiresAt:S,accessTime:N};i(this,h).set(e,x),w(this,p)._++;return}if(d==="symbol"){let K=f?o(this,n,j).call(this,e,f):void 0,x=Object(Symbol()),R={value:t,timeoutId:K,expiresAt:S,accessTime:N,unregisterToken:x};i(this,c).set(e,R),typeof t=="object"&&t!==null&&i(this,b).register(t,{key:e,type:"symbol"},x),w(this,p)._++;return}if(d==="object"&&!Array.isArray(t)&&t!==null){let K=new WeakRef(t),x=f?o(this,n,j).call(this,e,f):void 0,R=Object(Symbol()),B={weakRef:K,timeoutId:x,expiresAt:S,accessTime:N,unregisterToken:R};i(this,a).set(e,B),i(this,b).register(t,{key:e,type:"object"},R),w(this,p)._++}}getNotificationOnGC(e){let{key:t,value:s,unregisterToken:r,cleanup:d}=e;o(this,n,O).call(this,t,s),(d?new FinalizationRegistry(d):i(this,b)).register(s,{key:t,value:s,type:typeof s=="symbol"?"symbol":"object"},r)}delete(e){let t=i(this,g).get(e);t&&(o(this,n,D).call(this,t),i(this,g).delete(e));let s=!1;if(typeof e=="string"){let r=i(this,h).get(e);r&&(o(this,n,G).call(this,e,r),s=!0)}else if(typeof e=="symbol"){let r=i(this,c).get(e);r&&(o(this,n,H).call(this,e,r),s=!0)}else{let r=i(this,a).get(e);r&&(o(this,n,q).call(this,e,r),s=!0)}return s&&w(this,p)._--,s}has(e){if(typeof e=="string"){let s=i(this,h).get(e);return s?!o(this,n,y).call(this,s.expiresAt):!1}if(typeof e=="symbol"){let s=i(this,c).get(e);return s?!o(this,n,y).call(this,s.expiresAt):!1}let t=i(this,a).get(e);return!t||o(this,n,y).call(this,t.expiresAt)?!1:t.weakRef.deref()!==void 0}get size(){let e=0,t=[];for(let[s,r]of i(this,a))o(this,n,y).call(this,r.expiresAt)?t.push(s):r.weakRef.deref()!==void 0?e++:t.push(s);for(let[s,r]of i(this,c))o(this,n,y).call(this,r.expiresAt)?t.push(s):e++;for(let[s,r]of i(this,h))o(this,n,y).call(this,r.expiresAt)?t.push(s):e++;return t.forEach(s=>this.delete(s)),e}clear(){for(let e of i(this,a).values())e.timeoutId&&clearTimeout(e.timeoutId),i(this,b).unregister(e.unregisterToken);for(let e of i(this,c).values())e.timeoutId&&clearTimeout(e.timeoutId),i(this,b).unregister(e.unregisterToken);for(let e of i(this,h).values())e.timeoutId&&clearTimeout(e.timeoutId);i(this,a).clear(),i(this,c).clear(),i(this,h).clear(),i(this,g).clear(),E(this,p,0),i(this,T).next=i(this,l),i(this,l).prev=i(this,T)}keys(){let e=[],t=[];for(let[s,r]of i(this,a))o(this,n,y).call(this,r.expiresAt)?t.push(s):r.weakRef.deref()!==void 0?e.push(s):t.push(s);for(let[s,r]of i(this,c))o(this,n,y).call(this,r.expiresAt)?t.push(s):e.push(s);for(let[s,r]of i(this,h))o(this,n,y).call(this,r.expiresAt)?t.push(s):e.push(s);return t.forEach(s=>this.delete(s)),e}getTtl(e){let t=i(this,a).get(e)||i(this,c).get(e)||i(this,h).get(e);if(!t||!t.expiresAt)return null;let s=Date.now();return{ttl:Math.max(0,t.expiresAt-s),expiresAt:t.expiresAt}}updateTtl(e,t){let s=i(this,a).get(e),r=i(this,c).get(e),d=i(this,h).get(e);if(!s&&!r&&!d)return!1;let f=s||r||d;return f.timeoutId&&clearTimeout(f.timeoutId),f.timeoutId=o(this,n,j).call(this,e,t),f.expiresAt=Date.now()+t,!0}getStats(){return{size:i(this,p),stringItems:i(this,h).size,objectItems:i(this,a).size,symbolItems:i(this,c).size,lruNodes:i(this,g).size}}};h=new WeakMap,a=new WeakMap,c=new WeakMap,I=new WeakMap,v=new WeakMap,T=new WeakMap,l=new WeakMap,g=new WeakMap,p=new WeakMap,b=new WeakMap,n=new WeakSet,L=function(e){return typeof e=="string"?{entry:i(this,h).get(e),keyType:"string"}:typeof e=="symbol"?{entry:i(this,c).get(e),keyType:"symbol"}:{entry:i(this,a).get(e),keyType:"object"}},U=function(e){let t=i(this,g).get(e);if(t)t.next!==i(this,l)&&o(this,n,k).call(this,t);else{let s={key:e,prev:null,next:null};o(this,n,M).call(this,s),i(this,g).set(e,s)}},k=function(e){e.prev&&(e.prev.next=e.next),e.next&&(e.next.prev=e.prev),e.prev=i(this,l).prev,e.next=i(this,l),i(this,l).prev&&(i(this,l).prev.next=e),i(this,l).prev=e},M=function(e){e.prev=i(this,l).prev,e.next=i(this,l),i(this,l).prev&&(i(this,l).prev.next=e),i(this,l).prev=e},D=function(e){e.prev&&(e.prev.next=e.next),e.next&&(e.next.prev=e.prev)},J=function(){if(i(this,T).next===i(this,l))return;let e=i(this,T).next;e&&this.delete(e.key)},O=function(e,t){if(e==null)throw new Error("Key cannot be null or undefined");if(t==null)throw new Error("Value cannot be null or undefined")},V=function(e){this.delete(e.key)},W=function(e){this.delete(e)},j=function(e,t){return setTimeout(()=>{this.delete(e)},t)},y=function(e){return e!==void 0&&Date.now()>e},G=function(e,t){t.timeoutId&&clearTimeout(t.timeoutId),i(this,h).delete(e)},H=function(e,t){t.timeoutId&&clearTimeout(t.timeoutId),i(this,b).unregister(t.unregisterToken),i(this,c).delete(e)},q=function(e,t){t.timeoutId&&clearTimeout(t.timeoutId),i(this,b).unregister(t.unregisterToken),i(this,a).delete(e)};0&&(module.exports={SmartCache});