@jointly/cache-candidate 1.5.3 → 1.5.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -163,6 +163,7 @@ Please, refer to the [@jointly/cache-candidate-plugin-base](https://github.com/J
|
|
|
163
163
|
|
|
164
164
|
- [@jointly/cache-candidate-plugin-dependency-keys](https://github.com/JointlyTech/cache-candidate-plugin-dependency-keys): A plugin allowing to define dependency keys when setting the cache record.
|
|
165
165
|
This provides a mechanism to delete one or more cache records when a dependency key is invalidated.
|
|
166
|
+
- [@jointly/cache-candidate-plugin-invalidate-function](https://github.com/JointlyTech/cache-candidate-plugin-invalidate-function): A plugin providing an invalidation mechanism under specific conditions.
|
|
166
167
|
|
|
167
168
|
## Constraints
|
|
168
169
|
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var B=Object.create;var A=Object.defineProperty;var z=Object.getOwnPropertyDescriptor;var G=Object.getOwnPropertyNames;var V=Object.getPrototypeOf,Y=Object.prototype.hasOwnProperty;var F=(e,t)=>()=>(t||e((t={exports:{}}).exports,t),t.exports),Z=(e,t)=>{for(var n in t)A(e,n,{get:t[n],enumerable:!0})},S=(e,t,n,a)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of G(t))!Y.call(e,r)&&r!==n&&A(e,r,{get:()=>t[r],enumerable:!(a=z(t,r))||a.enumerable});return e};var P=(e,t,n)=>(n=e!=null?B(V(e)):{},S(t||!e||!e.__esModule?A(n,"default",{value:e,enumerable:!0}):n,e)),ee=e=>S(A({},"__esModule",{value:!0}),e);var R=F((Se,j)=>{"use strict";var _=Object.defineProperty,te=Object.getOwnPropertyDescriptor,ne=Object.getOwnPropertyNames,ae=Object.prototype.hasOwnProperty,re=(e,t)=>{for(var n in t)_(e,n,{get:t[n],enumerable:!0})},oe=(e,t,n,a)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of ne(t))!ae.call(e,r)&&r!==n&&_(e,r,{get:()=>t[r],enumerable:!(a=te(t,r))||a.enumerable});return e},ie=e=>oe(_({},"__esModule",{value:!0}),e),I={};re(I,{Hooks:()=>M});j.exports=ie(I);var M=(e=>(e.SETUP="SETUP",e.INIT="INIT",e.EXECUTION_PRE="EXECUTION_PRE",e.EXECUTION_POST="EXECUTION_POST",e.DATACACHE_RECORD_ADD_PRE="DATACACHE_RECORD_ADD_PRE",e.DATACACHE_RECORD_ADD_POST="DATACACHE_RECORD_ADD_POST",e.DATACACHE_RECORD_DELETE_PRE="DATACACHE_RECORD_DELETE_PRE",e.DATACACHE_RECORD_DELETE_POST="DATACACHE_RECORD_DELETE_POST",e.CACHE_HIT="CACHE_HIT",e))(M||{})});var q=F((Ne,Q)=>{"use strict";var H=Object.defineProperty,ce=Object.getOwnPropertyDescriptor,se=Object.getOwnPropertyNames,ue=Object.prototype.hasOwnProperty,de=(e,t)=>{for(var n in t)H(e,n,{get:t[n],enumerable:!0})},le=(e,t,n,a)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of se(t))!ue.call(e,r)&&r!==n&&H(e,r,{get:()=>t[r],enumerable:!(a=ce(t,r))||a.enumerable});return e},Ce=e=>le(H({},"__esModule",{value:!0}),e),U={};de(U,{Hook:()=>me,hook:()=>ge});Q.exports=Ce(U);var he={before:()=>{},after:()=>{}},fe={before:()=>{},after:()=>{}};function me(e){return function(t,n,a){let r={},o=a.value,{before:i,after:c}={...he,...e};return a.value=function(...s){i({context:r,args:s,target:t,propertyKey:n,descriptor:a});let d=o.apply(this,s);return c({context:r,args:s,target:t,propertyKey:n,descriptor:a,result:d}),d},a}}function ge(e){return function(t){let n={},{before:a,after:r}={...fe,...e};return function(...o){a({context:n,args:o});let i=t(...o);return r({context:n,args:o,result:i}),i}}}});var He={};Z(He,{CacheCandidate:()=>Oe,CacheCandidateOptionsDefault:()=>O,DataCacheRecordNotFound:()=>g,RunningQueryRecordNotFound:()=>D,cacheCandidate:()=>_e});module.exports=ee(He);var N=(e=new Map)=>({get:async t=>e.get(JSON.stringify(t)),has:async t=>e.has(JSON.stringify(t)),set:async(t,n)=>e.set(JSON.stringify(t),n),delete:async t=>e.delete(JSON.stringify(t))});var O={ttl:6e5,timeFrame:3e4,requestsThreshold:3,cache:N(),keepAlive:!1,plugins:[],events:{onCacheHit:e=>e,onCacheSet:e=>e,onCacheDelete:e=>e,onBeforeFunctionExecution:e=>e,onAfterFunctionExecution:e=>e}};var g=Symbol("DataCacheRecordNotFound"),D=Symbol("RunningQueryRecordNotFound");var k=P(R());var X=require("crypto");var J=P(R()),W=P(q());async function u(e,t,n){for(let a of t){let r=a.hooks.filter(i=>i.hook===e);if(r.length>1)throw new Error(`Only one hook instance per plugin is allowed. ${a.name} has ${r.length} instances of ${e}}`);if(r.length===0)continue;let o=r[0];if(pe(o))await o.action(n,a.additionalParameters);else throw new Error(`Hook ${e} for plugin ${a.name} is not actionable.`)}}function pe(e){return[e.action!==void 0,typeof e.action=="function"].every(t=>t===!0)}function $(e,t,n,a){return(0,W.hook)({before:()=>{u(e,n.plugins,a)},after:({result:r})=>{u(t,n.plugins,{...a,result:r})}})}function b({options:e}){e.plugins&&e.plugins.forEach(t=>{if(!t.hooks||!Array.isArray(t.hooks)||t.hooks.length===0)throw new Error(`Plugin ${t.name} has no hooks.`);t.hooks.forEach(n=>{if(!J.Hooks[n.hook])throw new Error(`Invalid hook ${n}`)})})}var C=P(R());function ye(e,t){return Date.now()<e+t.timeFrame}function Ee({key:e,timeframeCache:t,executionTime:n,executionEnd:a}){t.has(e)?t.get(e).push({executionTime:n,executionEnd:a}):t.set(e,[{executionEnd:a,executionTime:n}])}function Te({options:e,key:t,timeframeCache:n}){if(n.has(t)){let a=n.get(t),r=a.filter(({executionEnd:o})=>ye(o,e));a.length!==r.length&&n.set(t,r)}}function Ae({key:e,runningQueryCache:t}){return t.has(e)?t.get(e):D}function m(...e){return(0,X.createHash)("sha256").update(e.join("|")).digest("hex")}function y({birthTime:e,options:t}){return Date.now()-e>=t.ttl}async function p({options:e,key:t,HookPayload:n}){if(await e.cache.has(t)){let{result:a,birthTime:r}=await e.cache.get(t);return y({birthTime:r,options:e})?(await f({options:e,key:t,HookPayload:n}),g):a}return g}async function E({options:e,key:t,result:n,HookPayload:a}){return await u(C.Hooks.DATACACHE_RECORD_ADD_PRE,e.plugins,a),e.cache.set(t,{result:n,birthTime:Date.now()},e.ttl).then(async()=>{await u(C.Hooks.DATACACHE_RECORD_ADD_POST,e.plugins,{...a,result:n}),e.events.onCacheSet({key:t})})}async function f({options:e,key:t,HookPayload:n}){$(C.Hooks.DATACACHE_RECORD_DELETE_PRE,C.Hooks.DATACACHE_RECORD_DELETE_POST,e,n)(e.cache.delete)(t).then(()=>{e.events.onCacheDelete({key:t})})}async function L({result:e,runningQueryCache:t,key:n,executionStart:a,options:r,timeframeCache:o,timeoutCache:i,args:c,HookPayload:s}){let d=Date.now(),h=d-a;r.events.onAfterFunctionExecution({key:n,executionTime:h}),Ee({key:n,timeframeCache:o,executionTime:h,executionEnd:d}),await T({options:r,key:n,timeframeCache:o,executionTime:h,args:c})>=r.requestsThreshold?E({options:r,key:n,result:e,HookPayload:s}).finally(()=>{t.delete(n),i.set(n,setTimeout(()=>{f({options:r,key:n,HookPayload:s}),i.delete(n)},r.ttl).unref())}):t.delete(n)}async function T({options:e,key:t,timeframeCache:n,executionTime:a,args:r}){let o=0,i=n.get(t);return e.candidateFunction?o=await De(e,a,r,i):e.millisecondThreshold?o=Pe(a,e,i):o=i.length,o}function Pe(e,t,n){let a=0;return e>t.millisecondThreshold&&(a=n.filter(r=>r.executionTime>t.millisecondThreshold).length),a}async function De(e,t,n,a){let r=0;return await e.candidateFunction({timeFrameCacheRecords:a,options:e,args:n})&&(r=e.requestsThreshold),r}function Re({timeoutCache:e,key:t,options:n,HookPayload:a}){clearTimeout(e.get(t)),e.set(t,setTimeout(()=>{f({options:n,key:t,HookPayload:a}),e.delete(t)},n.ttl).unref())}function xe(e=10){return Math.random().toString(36).substring(2,e+2)}async function v({options:e,key:t,timeoutCache:n,runningQueryCache:a,timeframeCache:r,args:o,originalMethod:i}){let c={options:e,key:t,timeoutCache:n,runningQueryCache:a,timeframeCache:r,fnArgs:o,internals:{getDataCacheKey:m,getDataCacheRecord:p,addDataCacheRecord:E,deleteDataCacheRecord:f,isDataCacheRecordExpired:y,getExceedingAmount:T}};await u(C.Hooks.INIT,e.plugins,c);let s=await p({options:e,key:t,HookPayload:c});if(s!==g)return e.keepAlive&&Re({timeoutCache:n,key:t,options:e,HookPayload:c}),await u(C.Hooks.CACHE_HIT,e.plugins,{...c,result:s}),e.events.onCacheHit({key:t}),Promise.resolve(s);let d=Ae({key:t,runningQueryCache:a});if(d!==D)return await u(C.Hooks.CACHE_HIT,e.plugins,{...c,result:d}),d;Te({options:e,key:t,timeframeCache:r}),await u(C.Hooks.EXECUTION_PRE,e.plugins,c),e.events.onBeforeFunctionExecution({key:t});let h=Date.now(),l=i(...o);return await u(C.Hooks.EXECUTION_POST,e.plugins,{...c,result:l}),l instanceof Promise?(a.set(t,l),l.then(x=>L({result:x,runningQueryCache:a,key:t,executionStart:h,options:e,timeframeCache:r,timeoutCache:n,args:o,HookPayload:c})),l):(L({result:l,runningQueryCache:a,key:t,executionStart:h,options:e,timeframeCache:r,timeoutCache:n,args:o,HookPayload:c}),l)}function w(e){let t=new Map,n=new Map,a=xe(),r=new Map,o={...O,...e};return{timeframeCache:t,runningQueryCache:n,uniqueIdentifier:a,timeoutCache:r,options:o}}function Oe(e={}){let{timeframeCache:t,runningQueryCache:n,uniqueIdentifier:a,timeoutCache:r,options:o}=w(e);return b({options:o}),u(k.Hooks.SETUP,o.plugins,{options:{...o,plugins:void 0},key:"",timeoutCache:r,runningQueryCache:n,timeframeCache:t,fnArgs:[],internals:{getDataCacheKey:m,getDataCacheRecord:p,addDataCacheRecord:E,deleteDataCacheRecord:f,isDataCacheRecordExpired:y,getExceedingAmount:T}}),function(c,s,d){let h=d.value;d.value=async function(...l){let x=c.constructor.name+JSON.stringify(this),K=m([s,a,x,JSON.stringify(l)]);return v({options:o,key:K,timeoutCache:r,runningQueryCache:n,timeframeCache:t,args:l,originalMethod:h})}}}function _e(e,t={}){let{timeframeCache:n,runningQueryCache:a,uniqueIdentifier:r,timeoutCache:o,options:i}=w(t);return b({options:i}),u(k.Hooks.SETUP,i.plugins,{options:{...i,plugins:void 0},key:"",timeoutCache:o,runningQueryCache:a,timeframeCache:n,fnArgs:[],internals:{getDataCacheKey:m,getDataCacheRecord:p,addDataCacheRecord:E,deleteDataCacheRecord:f,isDataCacheRecordExpired:y,getExceedingAmount:T}}),function(...s){return v({options:i,key:m([r,JSON.stringify(s)]),timeoutCache:o,runningQueryCache:a,timeframeCache:n,args:s,originalMethod:e})}}0&&(module.exports={CacheCandidate,CacheCandidateOptionsDefault,DataCacheRecordNotFound,RunningQueryRecordNotFound,cacheCandidate});
|