@jointly/cache-candidate 1.5.0 → 1.5.2
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 +0 -1
- package/dist/index.js +1 -1
- package/dist/internal.d.ts +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -168,7 +168,6 @@ Please, refer to the [@jointly/cache-candidate-plugin-base](https://github.com/J
|
|
|
168
168
|
|
|
169
169
|
- The higher-order function and the decorator only work with async functions.
|
|
170
170
|
Please, refer to the [Considerations on synchronous functions](#considerations-on-synchronous-functions) section for more information.
|
|
171
|
-
- The `candidateFunction` must be synchronous. If passed an async function bypassing type checking, the candidateFunction will return a Promise thus not working properly. This choice was made to avoid the overhead and the performance burden of an async function call.
|
|
172
171
|
|
|
173
172
|
## Cache Stampede
|
|
174
173
|
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var K=Object.create;var A=Object.defineProperty;var B=Object.getOwnPropertyDescriptor;var z=Object.getOwnPropertyNames;var G=Object.getPrototypeOf,V=Object.prototype.hasOwnProperty;var F=(e,t)=>()=>(t||e((t={exports:{}}).exports,t),t.exports),Y=(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 z(t))!V.call(e,r)&&r!==n&&A(e,r,{get:()=>t[r],enumerable:!(a=B(t,r))||a.enumerable});return e};var D=(e,t,n)=>(n=e!=null?K(G(e)):{},S(t||!e||!e.__esModule?A(n,"default",{value:e,enumerable:!0}):n,e)),Z=e=>S(A({},"__esModule",{value:!0}),e);var O=F((Fe,j)=>{"use strict";var _=Object.defineProperty,ee=Object.getOwnPropertyDescriptor,te=Object.getOwnPropertyNames,ne=Object.prototype.hasOwnProperty,ae=(e,t)=>{for(var n in t)_(e,n,{get:t[n],enumerable:!0})},re=(e,t,n,a)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of te(t))!ne.call(e,r)&&r!==n&&_(e,r,{get:()=>t[r],enumerable:!(a=ee(t,r))||a.enumerable});return e},oe=e=>re(_({},"__esModule",{value:!0}),e),I={};ae(I,{Hooks:()=>M});j.exports=oe(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((Se,Q)=>{"use strict";var H=Object.defineProperty,ie=Object.getOwnPropertyDescriptor,ce=Object.getOwnPropertyNames,se=Object.prototype.hasOwnProperty,ue=(e,t)=>{for(var n in t)H(e,n,{get:t[n],enumerable:!0})},de=(e,t,n,a)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of ce(t))!se.call(e,r)&&r!==n&&H(e,r,{get:()=>t[r],enumerable:!(a=ie(t,r))||a.enumerable});return e},le=e=>de(H({},"__esModule",{value:!0}),e),U={};ue(U,{Hook:()=>fe,hook:()=>me});Q.exports=le(U);var Ce={before:()=>{},after:()=>{}},he={before:()=>{},after:()=>{}};function fe(e){return function(t,n,a){let r={},o=a.value,{before:i,after:c}={...Ce,...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 me(e){return function(t){let n={},{before:a,after:r}={...he,...e};return function(...o){a({context:n,args:o});let i=t(...o);return r({context:n,args:o,result:i}),i}}}});var _e={};Y(_e,{CacheCandidate:()=>xe,CacheCandidateOptionsDefault:()=>R,DataCacheRecordNotFound:()=>g,RunningQueryRecordNotFound:()=>P,cacheCandidate:()=>Re});module.exports=Z(_e);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 R={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"),P=Symbol("RunningQueryRecordNotFound");var
|
|
1
|
+
"use strict";var K=Object.create;var A=Object.defineProperty;var B=Object.getOwnPropertyDescriptor;var z=Object.getOwnPropertyNames;var G=Object.getPrototypeOf,V=Object.prototype.hasOwnProperty;var F=(e,t)=>()=>(t||e((t={exports:{}}).exports,t),t.exports),Y=(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 z(t))!V.call(e,r)&&r!==n&&A(e,r,{get:()=>t[r],enumerable:!(a=B(t,r))||a.enumerable});return e};var D=(e,t,n)=>(n=e!=null?K(G(e)):{},S(t||!e||!e.__esModule?A(n,"default",{value:e,enumerable:!0}):n,e)),Z=e=>S(A({},"__esModule",{value:!0}),e);var O=F((Fe,j)=>{"use strict";var _=Object.defineProperty,ee=Object.getOwnPropertyDescriptor,te=Object.getOwnPropertyNames,ne=Object.prototype.hasOwnProperty,ae=(e,t)=>{for(var n in t)_(e,n,{get:t[n],enumerable:!0})},re=(e,t,n,a)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of te(t))!ne.call(e,r)&&r!==n&&_(e,r,{get:()=>t[r],enumerable:!(a=ee(t,r))||a.enumerable});return e},oe=e=>re(_({},"__esModule",{value:!0}),e),I={};ae(I,{Hooks:()=>M});j.exports=oe(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((Se,Q)=>{"use strict";var H=Object.defineProperty,ie=Object.getOwnPropertyDescriptor,ce=Object.getOwnPropertyNames,se=Object.prototype.hasOwnProperty,ue=(e,t)=>{for(var n in t)H(e,n,{get:t[n],enumerable:!0})},de=(e,t,n,a)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of ce(t))!se.call(e,r)&&r!==n&&H(e,r,{get:()=>t[r],enumerable:!(a=ie(t,r))||a.enumerable});return e},le=e=>de(H({},"__esModule",{value:!0}),e),U={};ue(U,{Hook:()=>fe,hook:()=>me});Q.exports=le(U);var Ce={before:()=>{},after:()=>{}},he={before:()=>{},after:()=>{}};function fe(e){return function(t,n,a){let r={},o=a.value,{before:i,after:c}={...Ce,...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 me(e){return function(t){let n={},{before:a,after:r}={...he,...e};return function(...o){a({context:n,args:o});let i=t(...o);return r({context:n,args:o,result:i}),i}}}});var _e={};Y(_e,{CacheCandidate:()=>xe,CacheCandidateOptionsDefault:()=>R,DataCacheRecordNotFound:()=>g,RunningQueryRecordNotFound:()=>P,cacheCandidate:()=>Re});module.exports=Z(_e);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 R={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"),P=Symbol("RunningQueryRecordNotFound");var k=D(O());var X=require("crypto");var J=D(O()),$=D(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(ge(o))await o.action(n,a.additionalParameters);else throw new Error(`Hook ${e} for plugin ${a.name} is not actionable.`)}}function ge(e){return[e.action!==void 0,typeof e.action=="function"].every(t=>t===!0)}function L(e,t,n,a){return(0,$.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=D(O());function pe(e,t){return Date.now()<e+t.timeFrame}function ye({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 Ee({options:e,key:t,timeframeCache:n}){if(n.has(t)){let a=n.get(t),r=a.filter(({executionEnd:o})=>pe(o,e));a.length!==r.length&&n.set(t,r)}}function Te({key:e,runningQueryCache:t}){return t.has(e)?t.get(e):P}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}){L(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 W({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}),ye({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=Ae(a,e,i):o=i.length,o}function Ae(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 Pe({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 Oe(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&&Pe({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=Te({key:t,runningQueryCache:a});if(d!==P)return await u(C.Hooks.CACHE_HIT,e.plugins,{...c,result:d}),d;Ee({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=>W({result:x,runningQueryCache:a,key:t,executionStart:h,options:e,timeframeCache:r,timeoutCache:n,args:o,HookPayload:c})),l):(W({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=Oe(),r=new Map,o={...R,...e};return{timeframeCache:t,runningQueryCache:n,uniqueIdentifier:a,timeoutCache:r,options:o}}function xe(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(i,c,s){let d=s.value;s.value=async function(...h){let l=i.constructor.name+JSON.stringify(this),x=m([c,a,l,JSON.stringify(h)]);return v({options:o,key:x,timeoutCache:r,runningQueryCache:n,timeframeCache:t,args:h,originalMethod:d})}}}function Re(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}}),async(...c)=>v({options:i,key:m([r,JSON.stringify(c)]),timeoutCache:o,runningQueryCache:a,timeframeCache:n,args:c,originalMethod:e})}0&&(module.exports={CacheCandidate,CacheCandidateOptionsDefault,DataCacheRecordNotFound,RunningQueryRecordNotFound,cacheCandidate});
|
package/dist/internal.d.ts
CHANGED
|
@@ -27,7 +27,7 @@ export declare function getExceedingAmount({ options, key, timeframeCache, execu
|
|
|
27
27
|
timeframeCache: any;
|
|
28
28
|
executionTime: any;
|
|
29
29
|
args: any;
|
|
30
|
-
}): number
|
|
30
|
+
}): Promise<number>;
|
|
31
31
|
export declare function uniqid(length?: number): string;
|
|
32
32
|
export declare function letsCandidate({ options, key, timeoutCache, runningQueryCache, timeframeCache, args, originalMethod }: {
|
|
33
33
|
options: CacheCandidateOptions;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jointly/cache-candidate",
|
|
3
3
|
"private": false,
|
|
4
|
-
"version": "1.5.
|
|
4
|
+
"version": "1.5.2",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.js",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
"devDependencies": {
|
|
35
35
|
"@commitlint/cli": "^17.3.0",
|
|
36
36
|
"@commitlint/config-conventional": "^17.3.0",
|
|
37
|
-
"@jointly/cache-candidate-plugin-base": "^1.2.
|
|
37
|
+
"@jointly/cache-candidate-plugin-base": "^1.2.6",
|
|
38
38
|
"@types/jest": "^29.2.5",
|
|
39
39
|
"@types/node": "^18.11.12",
|
|
40
40
|
"@types/redis": "^4.0.11",
|