Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@swarmy/lru-cache

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@swarmy/lru-cache - npm Package Compare versions

Comparing version 3.1.1 to 3.1.2

2

build/lru-cache.js

@@ -1,1 +0,1 @@

module.exports=function(t){var r={};function u(e){if(r[e])return r[e].exports;var n=r[e]={i:e,l:!1,exports:{}};return t[e].call(n.exports,n,n.exports,u),n.l=!0,n.exports}return u.m=t,u.c=r,u.d=function(e,n,t){u.o(e,n)||Object.defineProperty(e,n,{enumerable:!0,get:t})},u.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},u.t=function(n,e){if(1&e&&(n=u(n)),8&e)return n;if(4&e&&"object"==typeof n&&n&&n.__esModule)return n;var t=Object.create(null);if(u.r(t),Object.defineProperty(t,"default",{enumerable:!0,value:n}),2&e&&"string"!=typeof n)for(var r in n)u.d(t,r,function(e){return n[e]}.bind(null,r));return t},u.n=function(e){var n=e&&e.__esModule?function(){return e.default}:function(){return e};return u.d(n,"a",n),n},u.o=function(e,n){return Object.prototype.hasOwnProperty.call(e,n)},u.p="",u(u.s=0)}([function(e,n,t){"use strict";t.r(n);var l=function(e,n){null===n.newest?(n.newest=e,n.oldest=e):((n.newest.next=e).prev=n.newest,n.newest=e)},i=function(e,n){e!==n.newest&&(null===(n.newest.next=e).prev?(e.prev=n.newest,e.next.prev=null,n.oldest=e.next):(e.prev.next=e.next,e.next.prev=e.prev,e.prev=n.newest),e.next=null,n.newest=e)},r=function(e,n,t){null===e.next?n.newest=e.prev:e.next.prev=e.prev,null===e.prev?n.oldest=e.next:e.prev.next=e.next,t.delete(e.key)},c=function(e,n,t){var r,u,o,a=[];if(null===e)return a;for(;t.size>e;)a.push((u=t,void 0,o=(r=n).oldest,u.delete(r.oldest.key),r.oldest=r.oldest.next,r.oldest.prev=null,{key:o.key,value:o.value}));return a};function f(){var e=0<arguments.length&&void 0!==arguments[0]?arguments[0]:null,n=this instanceof f?this:Object.create(f.prototype),u=e;0===u&&(u=null);var o=new Map,a={newest:null,oldest:null};return n.setMaxSize=function(e){return(u=e)<1&&(u=null),c(u,a,o)},n.getMaxSize=function(){return u},n.getSize=function(){return o.size},n.set=function(e,n){var t=o.get(e);if(void 0!==t)return t.value=n,i(t,a),null;t={key:e,value:n,next:null,prev:null},o.set(e,t),l(t,a);var r=c(u,a,o);return 1===r.length?r[0]:null},n.get=function(e){var n=o.get(e);return void 0===n?n:(i(n,a),n.value)},n.getWithoutLruChange=function(e){var n=o.get(e);return void 0===n?n:n.value},n.has=function(e){return o.has(e)},n.delete=function(e){var n=o.get(e);return void 0!==n&&(r(n,a,o),!0)},n.forEach=function(e){for(var n=a.oldest;null!==n;)e(n.value,n.key),n=n.next},n.map=function(e){for(var n=a.oldest,t=[];null!==n;)t.push(e(n.value,n.key)),n=n.next;return t},n.clear=function(){var e=n.map(function(e,n){return{key:n,value:e}});return o.clear(),a.newest=null,a.oldest=null,e},n}function y(e){return function(e){if(Array.isArray(e)){for(var n=0,t=new Array(e.length);n<e.length;n++)t[n]=e[n];return t}}(e)||function(e){if(Symbol.iterator in Object(e)||"[object Arguments]"===Object.prototype.toString.call(e))return Array.from(e)}(e)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance")}()}function a(n){for(var e=1;e<arguments.length;e++){var t=null!=arguments[e]?arguments[e]:{},r=Object.keys(t);"function"==typeof Object.getOwnPropertySymbols&&(r=r.concat(Object.getOwnPropertySymbols(t).filter(function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),r.forEach(function(e){s(n,e,t[e])})}return n}function s(e,n,t){return n in e?Object.defineProperty(e,n,{value:t,enumerable:!0,configurable:!0,writable:!0}):e[n]=t,e}t.d(n,"registerCacheChangedHandler",function(){return w}),t.d(n,"cacheTransaction",function(){return E}),t.d(n,"getCache",function(){return K}),t.d(n,"clearAllCaches",function(){return L});var v=500,u=0,d=new Map,h=new Map,p=new Set,o=function(t,e){null===e||Array.isArray(e)&&0===e.length?p.add(t):(Array.isArray(e)?e:[e]).forEach(function(e){var n=h.get(e);void 0===n&&(n=new Set,h.set(e,n)),n.add(t)})},g=function(n,e){null===e||Array.isArray(e)&&0===e.length?p.delete(n):(Array.isArray(e)?e:[e]).forEach(function(e){h.get(e).delete(n)})},w=function(e){var n=1<arguments.length&&void 0!==arguments[1]?arguments[1]:null,t=u;u+=1;var r={changedHandler:e,valueTypes:n,isActive:!0,unregister:function(){d.delete(t),g(t,n)},activate:function(){r.isActive=!0,o(t,n)},deactivate:function(){r.isActive=!1,g(t,n)},isRegistered:function(){return d.has(t)}};return d.set(t,r),o(t,n),r},m=function(t){var e,n,r,u=(e=t.valueTypes,n="string"==typeof e?[e]:e,r=new Set,n.forEach(function(e){var n=h.get(e);void 0!==n&&n.forEach(function(e){r.add(e)})}),p.forEach(function(e){r.add(e)}),r),o=[],a=0;if(u.forEach(function(e){var n=d.get(e);try{n.changedHandler(t)}catch(e){o.push(e)}finally{a+=1}}),0<o.length){var l="handleTransactionChangeObject: "+String(o.length)+" of "+String(a)+" handlers threw an error: ";o.forEach(function(e){l+=e.message+", "});var i=new Error(l);throw i.errors=o,i}},b=null,A=0,x=0,E=function(e){if(null===b&&(b={valueTypes:new Set}),x+=1,"function"==typeof e.finally)e.finally(function(){if(0===(x-=1))try{m(b)}finally{b=null,A=0}});else try{e()}finally{if(0===(x-=1))try{m(b)}finally{b=null,A=0}}},S=function(n,e,t,r){var u=b,o=null!==u;null===u&&(u={valueTypes:new Set}),u.valueTypes.has(n)?u[n][t].push(a({},e,{order:A++})):(u.valueTypes.add(n),u[n]=s({},t,[a({},e,{order:A++})]),r.forEach(function(e){u[n][e]=[]})),o||m(u)},O=!0,j=function(e,n){O&&S(e,n,"clearRemoves",["inserts","lruRemoves","deleteRemoves"])},k=function(e,n){O&&S(e,n,"lruRemoves",["clearRemoves","inserts","deleteRemoves"])},M=function(e,n){O&&S(e,n,"deleteRemoves",["clearRemoves","lruRemoves","inserts"])},R=function(u){return function(){for(var e=arguments.length,r=new Array(e),n=0;n<e;n++)r[n]=arguments[n];return new Promise(function(n,t){setTimeout(function(){try{var e=u.apply(void 0,r);n(e)}catch(e){t(e)}},0)})}},z=function(e,n){if(function(e){if(0<p.size)return!0;var n=h.get(e);return void 0!==n&&0<n.size}(e))E(n);else try{O=!1,n()}finally{O=!0}},C=function(c,f,s,e,v){if(!Array.isArray(e))throw new Error("LruCache::setAll: keyValueAlternateKeysArray must be an array");z(c,function(){e.forEach(function(e){var n=e.key,t=e.value,r=e.alternateKeys,u=f.getWithoutLruChange(n),o=Array.isArray(r)?r:[];0===o.length&&"string"==typeof r&&(o=[r]),o.forEach(function(e){if(s.has(e)&&s.get(e)!==n)throw new Error("LruCache::setAll: alternate key '"+e+"' is given for key '"+n+"' and value type '"+c+"' but is already used for key '"+s.get(e)+"'")}),o=new Set(o),void 0===u?u={key:n,value:t,alternateKeys:o}:(u.value=t,u.alternateKeys=new Set(y(u.alternateKeys).concat(y(o))));var a,l,i=f.set(n,u);o.forEach(function(e){s.set(e,n)}),a=c,l=u,O&&S(a,l,"inserts",["clearRemoves","lruRemoves","deleteRemoves"]),null!==i&&(i.value.alternateKeys.forEach(function(e){s.delete(e)}),v&&k(c,i.value))})})};function T(t){var e=1<arguments.length&&void 0!==arguments[1]?arguments[1]:v,a=this instanceof T?this:Object.create(T.prototype),r=f(e),l=new Map,u=!1,n=!1;a.dispatchLruRemoves=function(e){u=e},a.dispatchClearRemoves=function(e){n=e},a.setAll=function(e){C(t,r,l,e,u)},a.setAllAsync=R(a.setAll),a.set=function(e){a.setAll([e])},a.setAsync=R(a.set);var i=null,c=new Map,o=function(n,e){var t=2<arguments.length&&void 0!==arguments[2]&&arguments[2],r=e(n);if(void 0===r&&l.has(n))r=e(l.get(n));else if(void 0===r){if(t&&null!==i){if(c.has(n))return c.get(n);var u=i(n);if(!u)return r;if("function"!=typeof u.then)return a.set(u),u.value;var o=u.then(function(e){return e?(c.has(n)&&a.set(e),e.value):e}).finally(function(){c.delete(n)});return c.set(n,o),o}return r}return r.value};return a.setEntryGetter=function(e){i=e},a.get=function(e){return o(e,r.get,!0)},a.getWithoutLruChange=function(e){return o(e,r.getWithoutLruChange,!0)},a.has=function(e){return!!r.has(e)||l.has(e)},a.delete=function(e){var n=r.getWithoutLruChange(e);return void 0===n?(z(t,function(){M(t,{key:e})}),!1):(r.delete(n.key),n.alternateKeys.forEach(function(e){l.delete(e)}),z(t,function(){M(t,{key:e})}),!0)},a.forEach=r.forEach,a.getEntries=function(){return r.map(function(e){return e})},a.clear=function(){var e=r.clear();l.clear(),n&&z(t,function(){e.forEach(function(e){j(t,e.value)})})},a.getSize=r.getSize,a.getValueType=function(){return t},a.getMaxSize=r.getMaxSize,a.setMaxSize=function(e){var n=r.setMaxSize(e);z(t,function(){n.forEach(function(e){e.value.alternateKeys.forEach(function(e){l.delete(e)}),u&&k(t,e.value)})})},a}var P=new Map,K=function(e){var n=P.get(e);return void 0===n&&(n=T(e),P.set(e,n)),n},L=function(){E(function(){P.forEach(function(e){e.clear()})})}}]);
module.exports=function(n){var r={};function u(e){if(r[e])return r[e].exports;var t=r[e]={i:e,l:!1,exports:{}};return n[e].call(t.exports,t,t.exports,u),t.l=!0,t.exports}return u.m=n,u.c=r,u.d=function(e,t,n){u.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:n})},u.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},u.t=function(t,e){if(1&e&&(t=u(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var n=Object.create(null);if(u.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var r in t)u.d(n,r,function(e){return t[e]}.bind(null,r));return n},u.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return u.d(t,"a",t),t},u.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},u.p="",u(u.s=0)}([function(e,t,n){"use strict";n.r(t);var l=function(e,t){null===t.newest?(t.newest=e,t.oldest=e):((t.newest.next=e).prev=t.newest,t.newest=e)},i=function(e,t){e!==t.newest&&(null===(t.newest.next=e).prev?(e.prev=t.newest,e.next.prev=null,t.oldest=e.next):(e.prev.next=e.next,e.next.prev=e.prev,e.prev=t.newest),e.next=null,t.newest=e)},r=function(e,t,n){null===e.next?t.newest=e.prev:e.next.prev=e.prev,null===e.prev?t.oldest=e.next:e.prev.next=e.next,n.delete(e.key)},c=function(e,t,n){var r,u,o,a=[];if(null===e)return a;for(;n.size>e;)a.push((u=n,void 0,o=(r=t).oldest,u.delete(r.oldest.key),r.oldest=r.oldest.next,r.oldest.prev=null,{key:o.key,value:o.value}));return a};function s(){var e=0<arguments.length&&void 0!==arguments[0]?arguments[0]:null,t=this instanceof s?this:Object.create(s.prototype),u=e;0===u&&(u=null);var o=new Map,a={newest:null,oldest:null};return t.setMaxSize=function(e){return(u=e)<1&&(u=null),c(u,a,o)},t.getMaxSize=function(){return u},t.getSize=function(){return o.size},t.set=function(e,t){var n=o.get(e);if(void 0!==n)return n.value=t,i(n,a),null;n={key:e,value:t,next:null,prev:null},o.set(e,n),l(n,a);var r=c(u,a,o);return 1===r.length?r[0]:null},t.get=function(e){var t=o.get(e);return void 0===t?t:(i(t,a),t.value)},t.getWithoutLruChange=function(e){var t=o.get(e);return void 0===t?t:t.value},t.has=function(e){return o.has(e)},t.delete=function(e){var t=o.get(e);return void 0!==t&&(r(t,a,o),!0)},t.forEach=function(e){for(var t=a.oldest;null!==t;)e(t.value,t.key),t=t.next},t.map=function(e){for(var t=a.oldest,n=[];null!==t;)n.push(e(t.value,t.key)),t=t.next;return n},t.clear=function(){var e=t.map(function(e,t){return{key:t,value:e}});return o.clear(),a.newest=null,a.oldest=null,e},t}function d(e){return function(e){if(Array.isArray(e)){for(var t=0,n=new Array(e.length);t<e.length;t++)n[t]=e[t];return n}}(e)||function(e){if(Symbol.iterator in Object(e)||"[object Arguments]"===Object.prototype.toString.call(e))return Array.from(e)}(e)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance")}()}function a(t){for(var e=1;e<arguments.length;e++){var n=null!=arguments[e]?arguments[e]:{},r=Object.keys(n);"function"==typeof Object.getOwnPropertySymbols&&(r=r.concat(Object.getOwnPropertySymbols(n).filter(function(e){return Object.getOwnPropertyDescriptor(n,e).enumerable}))),r.forEach(function(e){f(t,e,n[e])})}return t}function f(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}n.d(t,"registerCacheChangedHandler",function(){return w}),n.d(t,"cacheTransaction",function(){return E}),n.d(t,"getCache",function(){return K}),n.d(t,"clearAllCaches",function(){return L});var v=500,u=0,y=new Map,h=new Map,g=new Set,o=function(n,e){null===e||Array.isArray(e)&&0===e.length?g.add(n):(Array.isArray(e)?e:[e]).forEach(function(e){var t=h.get(e);void 0===t&&(t=new Set,h.set(e,t)),t.add(n)})},p=function(t,e){null===e||Array.isArray(e)&&0===e.length?g.delete(t):(Array.isArray(e)?e:[e]).forEach(function(e){h.get(e).delete(t)})},w=function(e){var t=1<arguments.length&&void 0!==arguments[1]?arguments[1]:null,n=u;u+=1;var r={changedHandler:e,valueTypes:t,isActive:!0,unregister:function(){y.delete(n),p(n,t)},activate:function(){r.isActive=!0,o(n,t)},deactivate:function(){r.isActive=!1,p(n,t)},isRegistered:function(){return y.has(n)}};return y.set(n,r),o(n,t),r},m=function(n){var e,t,r,u=(e=n.valueTypes,t="string"==typeof e?[e]:e,r=new Set,t.forEach(function(e){var t=h.get(e);void 0!==t&&t.forEach(function(e){r.add(e)})}),g.forEach(function(e){r.add(e)}),r),o=[],a=0;if(u.forEach(function(e){var t=y.get(e);try{t.changedHandler(n)}catch(e){o.push(e)}finally{a+=1}}),0<o.length){var l="handleTransactionChangeObject: "+String(o.length)+" of "+String(a)+" handlers threw an error: ";o.forEach(function(e){l+=e.message+", "});var i=new Error(l);throw i.errors=o,i}},b=null,A=0,x=0,E=function(e){if(null===b&&(b={valueTypes:new Set}),x+=1,"function"==typeof e.finally)e.finally(function(){if(0===(x-=1))try{m(b)}finally{b=null,A=0}});else try{e()}finally{if(0===(x-=1))try{m(b)}finally{b=null,A=0}}},S=function(t,e,n,r){var u=b,o=null!==u;null===u&&(u={valueTypes:new Set}),u.valueTypes.has(t)?u[t][n].push(a({},e,{order:A++})):(u.valueTypes.add(t),u[t]=f({},n,[a({},e,{order:A++})]),r.forEach(function(e){u[t][e]=[]})),o||m(u)},O=!0,j=function(e,t){O&&S(e,t,"clearRemoves",["inserts","lruRemoves","deleteRemoves"])},k=function(e,t){O&&S(e,t,"lruRemoves",["clearRemoves","inserts","deleteRemoves"])},M=function(e,t){O&&S(e,t,"deleteRemoves",["clearRemoves","lruRemoves","inserts"])},R=function(u){return function(){for(var e=arguments.length,r=new Array(e),t=0;t<e;t++)r[t]=arguments[t];return new Promise(function(t,n){setTimeout(function(){try{var e=u.apply(void 0,r);t(e)}catch(e){n(e)}},0)})}},C=function(e,t){if(function(e){if(0<g.size)return!0;var t=h.get(e);return void 0!==t&&0<t.size}(e))E(t);else try{O=!1,t()}finally{O=!0}},z=function(c,f,s,e,v){if(!Array.isArray(e))throw new Error("LruCache::setAll: keyValueAlternateKeysArray must be an array");C(c,function(){e.forEach(function(e){var t=e.key,n=e.value,r=e.alternateKeys,u=f.getWithoutLruChange(t),o=Array.isArray(r)?r:[];0===o.length&&"string"==typeof r&&(o=[r]),o.forEach(function(e){if(s.has(e)&&s.get(e)!==t)throw new Error("LruCache::setAll: alternate key '"+e+"' is given for key '"+t+"' and value type '"+c+"' but is already used for key '"+s.get(e)+"'")}),o=new Set(o),void 0===u?u={key:t,value:n,alternateKeys:o}:(u.value=n,u.alternateKeys=new Set(d(u.alternateKeys).concat(d(o))));var a,l,i=f.set(t,u);o.forEach(function(e){s.set(e,t)}),a=c,l=u,O&&S(a,l,"inserts",["clearRemoves","lruRemoves","deleteRemoves"]),null!==i&&(i.value.alternateKeys.forEach(function(e){s.delete(e)}),v&&k(c,i.value))})})};function T(n){var e=1<arguments.length&&void 0!==arguments[1]?arguments[1]:v,l=this instanceof T?this:Object.create(T.prototype),r=s(e),i=new Map,u=!1,t=!1;l.dispatchLruRemoves=function(e){u=e},l.dispatchClearRemoves=function(e){t=e},l.setAll=function(e){z(n,r,i,e,u)},l.setAllAsync=R(l.setAll),l.set=function(e){l.setAll([e])},l.setAsync=R(l.set);var c=null,f=new Map,o=function(t,e){var n,r=2<arguments.length&&void 0!==arguments[2]&&arguments[2],u=3<arguments.length&&void 0!==arguments[3]&&arguments[3];if(u||void 0===(n=e(t))&&i.has(t)&&(n=e(i.get(t))),void 0!==n)return n.value;if(r&&null!==c){if(f.has(t))return f.get(t);var o=c(t);if(!o)return n;if("function"!=typeof o.then)return l.set(o),o.value;var a=o.then(function(e){return e?(f.has(t)&&l.set(e),e.value):e}).finally(function(){f.delete(t)});return f.set(t,a),a}if(u)throw new Error("called get with notFromCache, but no entry getter was set");return n};return l.setEntryGetter=function(e){c=e},l.get=function(e){var t=1<arguments.length&&void 0!==arguments[1]&&arguments[1];return o(e,r.get,!0,t)},l.getWithoutLruChange=function(e){var t=1<arguments.length&&void 0!==arguments[1]&&arguments[1];return o(e,r.getWithoutLruChange,!0,t)},l.has=function(e){return!!r.has(e)||i.has(e)},l.delete=function(e){var t=r.getWithoutLruChange(e);return void 0===t?(C(n,function(){M(n,{key:e})}),!1):(r.delete(t.key),t.alternateKeys.forEach(function(e){i.delete(e)}),C(n,function(){M(n,{key:e})}),!0)},l.forEach=r.forEach,l.getEntries=function(){return r.map(function(e){return e})},l.clear=function(){var e=r.clear();i.clear(),t&&C(n,function(){e.forEach(function(e){j(n,e.value)})})},l.getSize=r.getSize,l.getValueType=function(){return n},l.getMaxSize=r.getMaxSize,l.setMaxSize=function(e){var t=r.setMaxSize(e);C(n,function(){t.forEach(function(e){e.value.alternateKeys.forEach(function(e){i.delete(e)}),u&&k(n,e.value)})})},l}var P=new Map,K=function(e){var t=P.get(e);return void 0===t&&(t=T(e),P.set(e,t)),t},L=function(){E(function(){P.forEach(function(e){e.clear()})})}}]);
{
"name": "@swarmy/lru-cache",
"version": "3.1.1",
"version": "3.1.2",
"description": "LRU cache with event registry to handle cache changes",

@@ -5,0 +5,0 @@ "author": "Gerd Neudert",

@@ -72,3 +72,3 @@ # lru-cache

* [Detailed Usage](#caching-detail)
* [JSDoc](https://rawcdn.githack.com/gneu77/lru-cache/0eca7d9be9c3901371f27ca08fd2c9c00adde925/docs/index.html)
* [JSDoc](https://rawcdn.githack.com/gneu77/lru-cache/6e4c5425a8aaf7c311dad8ccd1a4150285a38b0e/docs/index.html)

@@ -86,8 +86,8 @@ ### Cache Events

* [Detailed Usage](#cache-events-detail)
* [JSDoc](https://rawcdn.githack.com/gneu77/lru-cache/0eca7d9be9c3901371f27ca08fd2c9c00adde925/docs/index.html)
* [JSDoc](https://rawcdn.githack.com/gneu77/lru-cache/6e4c5425a8aaf7c311dad8ccd1a4150285a38b0e/docs/index.html)
## Quality <a name="section-quality"></a>
* [Test results](https://rawcdn.githack.com/gneu77/lru-cache/0eca7d9be9c3901371f27ca08fd2c9c00adde925/test-report.html)
* [Test coverage](https://rawcdn.githack.com/gneu77/lru-cache/0eca7d9be9c3901371f27ca08fd2c9c00adde925/coverage/index.html)
* [Performance tests](https://rawcdn.githack.com/gneu77/lru-cache/0eca7d9be9c3901371f27ca08fd2c9c00adde925/performance-report.html)
* [Test results](https://rawcdn.githack.com/gneu77/lru-cache/6e4c5425a8aaf7c311dad8ccd1a4150285a38b0e/test-report.html)
* [Test coverage](https://rawcdn.githack.com/gneu77/lru-cache/6e4c5425a8aaf7c311dad8ccd1a4150285a38b0e/coverage/index.html)
* [Performance tests](https://rawcdn.githack.com/gneu77/lru-cache/6e4c5425a8aaf7c311dad8ccd1a4150285a38b0e/performance-report.html)

@@ -127,3 +127,3 @@ ## Develop <a name="section-develop"></a>

`forEach` | callback: function | undefined | Iterate over the cache from olodest to newest value. Callback receives cache entry as argument, being an object with `key`, `value` and `alternateKeys`.
`get` | keyOrAlternateKey: string | value | Get cached value or undefined. The returned value will be made the newest value in the cache. If an entry getter is set (see `setEntryGetter`), this getter will be used in case of a cache miss. If the entry getter is an async function, then a Promise will be returned that resolves to the value (Subsequent calls to get will return the same Promise until resolved, so the entry getter is called only once).
`get` | keyOrAlternateKey: string, notFromCache: boolean (default: false) | value | Get cached value or undefined. The returned value will be made the newest value in the cache. If an entry getter is set (see `setEntryGetter`), this getter will be used in case of a cache miss. If the entry getter is an async function, then a Promise will be returned that resolves to the value (Subsequent calls to get will return the same Promise until resolved, so the entry getter is called only once). If notFromCache is set true, the value will be taken from entry getter, even if a value is already cached. If in this case no entry getter was set, a corresponding error will bethrown.
`getEntries` | none | Array | Returns an array with all cache entries, order from oldest to newest.

@@ -133,3 +133,3 @@ `getMaxSize` | none | Int | Returns the current max size of the cache.

`getValueType` | none | string | Returns the value type of the cache.
`getWithoutLruChange` | keyOrAlternateKey: string | value | Like `get`, but without making the entry the newest in the cache.
`getWithoutLruChange` | keyOrAlternateKey: string, notFromCache: boolean (default: false) | value | Like `get`, but without making the entry the newest in the cache.
`has` | keyOrAlternateKey: string | isInCache: boolean | True, if the key or alternate key is in the cache.

@@ -178,6 +178,6 @@ `set` | keyValueAlternateKeys: object | undefined | Insert or update a value in the cache. The argument must be an object with `key` and `value`. Optionally it can also have `alternateKeys`, being string or array of strings. After set, the value will be the newest in the cache. Dispatches a corresponding cache event. If an insert exceeds the max cache size and dispatchLruRemoves is set true, it will be included in the event.

* Also compared to LRU maps that do not support alternate keys, there is a performance impact on get in case of cache misses.
* See [Performance tests](https://rawcdn.githack.com/gneu77/lru-cache/0eca7d9be9c3901371f27ca08fd2c9c00adde925/performance-report.html)
* See [Performance tests](https://rawcdn.githack.com/gneu77/lru-cache/6e4c5425a8aaf7c311dad8ccd1a4150285a38b0e/performance-report.html)
* However, get, set and delete are still O(1). (setMaxSize has O(size-newMaxSize), if size>newMaxSize)
* Compared to a LRU cache without cache events, there is additional performance impact on get, set and delete.
* Again see [Performance tests](https://rawcdn.githack.com/gneu77/lru-cache/0eca7d9be9c3901371f27ca08fd2c9c00adde925/performance-report.html)
* Again see [Performance tests](https://rawcdn.githack.com/gneu77/lru-cache/6e4c5425a8aaf7c311dad8ccd1a4150285a38b0e/performance-report.html)
* However, if you are caching for performance, then because the fetching of values is significantly more time consuming. So whether you save 400ms or only 399ms hardly makes a difference here.

@@ -184,0 +184,0 @@ * If you are not caching for performance reasons, but to have the change events, well than again it's just the price to pay for the event handling.

@@ -403,8 +403,11 @@ import {LruMap} from "./LruMap";

const keyToPromise = new Map();
const internalGetter = (key, getter, useEntryGetter = false) => {
let entry = getter(key);
if (typeof entry === "undefined" && alternateKeyToKey.has(key)) {
entry = getter(alternateKeyToKey.get(key));
const internalGetter = (key, getter, useEntryGetter = false, notFromCache = false) => {
let entry; // eslint-disable-line init-declarations
if (!notFromCache) {
entry = getter(key);
if (typeof entry === "undefined" && alternateKeyToKey.has(key)) {
entry = getter(alternateKeyToKey.get(key));
}
}
else if (typeof entry === "undefined") {
if (typeof entry === "undefined") {
if (useEntryGetter && entryGetter !== null) {

@@ -439,2 +442,5 @@ if (keyToPromise.has(key)) {

}
else if (notFromCache) {
throw new Error("called get with notFromCache, but no entry getter was set");
}
else {

@@ -472,5 +478,8 @@ return entry;

* @param {string} keyOrAlternateKey - The key or alternate key of the value
* @param {boolean} notFromCache - If true and an entry getter is set, then the value will not be taken from the
* cache, but from the entry getter. If no entry getter is set, an error will be
* thrown. (default: false)
* @returns {value | Promise | undefined} value, promised value or undefined
*/
self.get = keyOrAlternateKey => internalGetter(keyOrAlternateKey, lruMap.get, true);
self.get = (keyOrAlternateKey, notFromCache = false) => internalGetter(keyOrAlternateKey, lruMap.get, true, notFromCache);

@@ -483,5 +492,8 @@ /** Like 'get', but not making the corresponding entry the most recently used.

* @param {string} keyOrAlternateKey - The key or alternate key of the value
* @param {boolean} notFromCache - If true and an entry getter is set, then the value will not be taken from the
* cache, but from the entry getter. If no entry getter is set, an error will be
* thrown. (default: false)
* @returns {value | Promise | undefined} value, promised value or undefined
*/
self.getWithoutLruChange = keyOrAlternateKey => internalGetter(keyOrAlternateKey, lruMap.getWithoutLruChange, true);
self.getWithoutLruChange = (keyOrAlternateKey, notFromCache = false) => internalGetter(keyOrAlternateKey, lruMap.getWithoutLruChange, true, notFromCache);

@@ -488,0 +500,0 @@ /** Return whether the cache contains an entry for the given key or alternate key

@@ -367,5 +367,7 @@ /* eslint-disable no-magic-number, max-lines */

expect(typeof cache.get("key1").then === "function").toBeTruthy();
const val = await cache.get("key1");
let val = await cache.get("key1");
expect(typeof val).toEqual("undefined");
expect(typeof cache.get("key1")).toEqual("object");
val = await cache.get("key1");
expect(typeof val).toEqual("undefined");
// We must also ensure that the getter was called only once:

@@ -378,2 +380,39 @@ expect(nCalls).toEqual(2);

it("should take the value from the entry getter and not from the cache, if specified in get", () => {
const type3GetterSync = key => ({
key,
value: key + "_value",
alternateKeys: [key + "_alternate"],
});
const cache = getCache(VALUE_TYPE_3);
cache.setEntryGetter(type3GetterSync);
cache.set({
key: "key1",
value: "value1",
});
expect(cache.get("key1")).toEqual("value1");
expect(cache.get("key1", true)).toEqual("key1_value");
expect(cache.get("key1")).toEqual("key1_value");
cache.setEntryGetter(null);
cache.clear();
});
it("should throw, if value should be taken from entry getter and no entry getter is defined", () => {
const cache = getCache(VALUE_TYPE_3);
cache.set({
key: "key1",
value: "value1",
});
expect(cache.get("key1")).toEqual("value1");
expect(() => {
cache.get("key1", true);
}).toThrow("called get with notFromCache, but no entry getter was set");
cache.clear();
});
});

@@ -380,0 +419,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc