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.2 to 3.2.0

0

.eslintrc.js

@@ -0,0 +0,0 @@ module.exports = {

2

build/lru-cache.js

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

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()})})}}]);
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 d(){var e=0<arguments.length&&void 0!==arguments[0]?arguments[0]:null,n=this instanceof d?this:Object.create(d.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){f(n,e,t[e])})}return n}function f(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 h=500,u=0,s=new Map,v=new Map,g=new Set,o=function(t,e){null===e||Array.isArray(e)&&0===e.length?g.add(t):(Array.isArray(e)?e:[e]).forEach(function(e){var n=v.get(e);void 0===n&&(n=new Set,v.set(e,n)),n.add(t)})},p=function(n,e){null===e||Array.isArray(e)&&0===e.length?g.delete(n):(Array.isArray(e)?e:[e]).forEach(function(e){v.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(){s.delete(t),p(t,n)},activate:function(){r.isActive=!0,o(t,n)},deactivate:function(){r.isActive=!1,p(t,n)},isRegistered:function(){return s.has(t)}};return s.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=v.get(e);void 0!==n&&n.forEach(function(e){r.add(e)})}),g.forEach(function(e){r.add(e)}),r),o=[],a=0;if(u.forEach(function(e){var n=s.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]=f({},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)})}},C=function(e,n){if(function(e){if(0<g.size)return!0;var n=v.get(e);return void 0!==n&&0<n.size}(e))E(n);else try{O=!1,n()}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 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]:h,c=this instanceof T?this:Object.create(T.prototype),r=d(e),f=new Map,u=!1,n=!1;c.dispatchLruRemoves=function(e){u=e},c.dispatchClearRemoves=function(e){n=e},c.setAll=function(e){z(t,r,f,e,u)},c.setAllAsync=R(c.setAll),c.set=function(e){c.setAll([e])},c.setAsync=R(c.set);var s=null,v=new Map,o=function(n,e){var t,r=2<arguments.length&&void 0!==arguments[2]&&arguments[2],u=3<arguments.length&&void 0!==arguments[3]&&arguments[3],o=4<arguments.length&&void 0!==arguments[4]?arguments[4]:null;u||void 0===(t=e(n))&&f.has(n)&&(t=e(f.get(n)));var a=s;if(null!==o&&(a=o),void 0!==t)return t.value;if(r&&null!==a){if(v.has(n))return v.get(n);var l=a(n);if(!l)return t;if("function"!=typeof l.then)return c.set(l),l.value;var i=l.then(function(e){return e?(v.has(n)&&c.set(e),e.value):e}).finally(function(){v.delete(n)});return v.set(n,i),i}if(u)throw new Error("called get with notFromCache, but no entry getter was set");return t};return c.setEntryGetter=function(e){s=e},c.get=function(e){var n=1<arguments.length&&void 0!==arguments[1]&&arguments[1],t=2<arguments.length&&void 0!==arguments[2]?arguments[2]:null;return o(e,r.get,!0,n,t)},c.getWithoutLruChange=function(e){var n=1<arguments.length&&void 0!==arguments[1]&&arguments[1],t=2<arguments.length&&void 0!==arguments[2]?arguments[2]:null;return o(e,r.getWithoutLruChange,!0,n,t)},c.has=function(e){return!!r.has(e)||f.has(e)},c.delete=function(e){var n=r.getWithoutLruChange(e);return void 0===n?(C(t,function(){M(t,{key:e})}),!1):(r.delete(n.key),n.alternateKeys.forEach(function(e){f.delete(e)}),C(t,function(){M(t,{key:e})}),!0)},c.forEach=r.forEach,c.getEntries=function(){return r.map(function(e){return e})},c.clear=function(){var e=r.clear();f.clear(),n&&C(t,function(){e.forEach(function(e){j(t,e.value)})})},c.getSize=r.getSize,c.getValueType=function(){return t},c.getMaxSize=r.getMaxSize,c.setMaxSize=function(e){var n=r.setMaxSize(e);C(t,function(){n.forEach(function(e){e.value.alternateKeys.forEach(function(e){f.delete(e)}),u&&k(t,e.value)})})},c}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()})})}}]);

@@ -0,0 +0,0 @@ // Jest configuration for functional tests

@@ -0,0 +0,0 @@ // Jest configuration for functional tests

{
"name": "@swarmy/lru-cache",
"version": "3.1.2",
"version": "3.2.0",
"description": "LRU cache with event registry to handle cache changes",

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

@@ -24,2 +24,6 @@ # lru-cache

## What's New <a name="section-news"></a>
### Version 3.2.0
* LruCache methods `get` and `getWithoutLruChange` now take custom entry getter as optional third argument.
* If provided, a custom entry getter takes precedence over one set by `setEntryGetter`
### Version 3.1.0

@@ -73,3 +77,3 @@ * New LruCache method `setEntryGetter`

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

@@ -87,8 +91,8 @@ ### Cache Events

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

@@ -128,3 +132,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, 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.
`get` | keyOrAlternateKey: string, notFromCache: boolean (default: false), customEntryGetter: function (default: null) | 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 be thrown. A custom entry getter can also be specified as optional third argument.
`getEntries` | none | Array | Returns an array with all cache entries, order from oldest to newest.

@@ -134,3 +138,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, notFromCache: boolean (default: false) | value | Like `get`, but without making the entry the newest in the cache.
`getWithoutLruChange` | keyOrAlternateKey: string, notFromCache: boolean (default: false), customEntryGetter: function (default: null) | 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.

@@ -179,6 +183,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/6e4c5425a8aaf7c311dad8ccd1a4150285a38b0e/performance-report.html)
* See [Performance tests](https://rawcdn.githack.com/gneu77/lru-cache/1b52feca07fbe0a44b8cdc991de3e2c7563e55bc/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/6e4c5425a8aaf7c311dad8ccd1a4150285a38b0e/performance-report.html)
* Again see [Performance tests](https://rawcdn.githack.com/gneu77/lru-cache/1b52feca07fbe0a44b8cdc991de3e2c7563e55bc/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.

@@ -200,3 +204,3 @@ * 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.

### 3.1.x
### 3.2.x
* Fix issues that might occur

@@ -207,6 +211,6 @@ * Improve README

### 3.2.0
### 3.3.0
* Add option to use LruMap instead of LruCache (the LruMap is a currently not exported pure LRU cache, while the LruCache is a wrapper adding the event handling)
### 3.3.0
### 3.4.0
* Make shape and content of change events configurable (with the current shape being used as default).

@@ -128,3 +128,3 @@ import {LruMap} from "./LruMap";

}
catch(error) {
catch (error) {
errors.push(error);

@@ -404,3 +404,3 @@ }

const keyToPromise = new Map();
const internalGetter = (key, getter, useEntryGetter = false, notFromCache = false) => {
const internalGetter = (key, getter, useEntryGetter = false, notFromCache = false, customEntryGetter = null) => {
let entry; // eslint-disable-line init-declarations

@@ -413,8 +413,12 @@ if (!notFromCache) {

}
let usedEntryGetter = entryGetter;
if (customEntryGetter !== null) {
usedEntryGetter = customEntryGetter;
}
if (typeof entry === "undefined") {
if (useEntryGetter && entryGetter !== null) {
if (useEntryGetter && usedEntryGetter !== null) {
if (keyToPromise.has(key)) {
return keyToPromise.get(key);
}
const keyValueAlternateKeys = entryGetter(key);
const keyValueAlternateKeys = usedEntryGetter(key);
if (!keyValueAlternateKeys) {

@@ -482,5 +486,7 @@ return entry;

* thrown. (default: false)
* @param {function} customEntryGetter - function that takes a key as argument and returns corresponding entry or
* promised entry. Has precedence over entry gettter set via setEntryGetter.
* @returns {value | Promise | undefined} value, promised value or undefined
*/
self.get = (keyOrAlternateKey, notFromCache = false) => internalGetter(keyOrAlternateKey, lruMap.get, true, notFromCache);
self.get = (keyOrAlternateKey, notFromCache = false, customEntryGetter = null) => internalGetter(keyOrAlternateKey, lruMap.get, true, notFromCache, customEntryGetter);

@@ -496,5 +502,7 @@ /** Like 'get', but not making the corresponding entry the most recently used.

* thrown. (default: false)
* @param {function} customEntryGetter - function that takes a key as argument and returns corresponding entry or
* promised entry. Has precedence over entry gettter set via setEntryGetter.
* @returns {value | Promise | undefined} value, promised value or undefined
*/
self.getWithoutLruChange = (keyOrAlternateKey, notFromCache = false) => internalGetter(keyOrAlternateKey, lruMap.getWithoutLruChange, true, notFromCache);
self.getWithoutLruChange = (keyOrAlternateKey, notFromCache = false, customEntryGetter = null) => internalGetter(keyOrAlternateKey, lruMap.getWithoutLruChange, true, notFromCache, customEntryGetter);

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

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

@@ -325,3 +325,3 @@ /* eslint-disable no-magic-number, max-lines */

it("should provide a method to define an entry async getter that is used in case of a cache miss", async () => {
it("should provide a method to define an async entry getter that is used in case of a cache miss", async () => {
let nCalls = 0;

@@ -417,2 +417,35 @@ const type3GetterAsync = key => new Promise(resolve => {

it("should be possible to override a set entry getter by passing a custom one to the get method", () => {
const type3GetterSync = key => ({
key,
value: key + "_value",
alternateKeys: [key + "_alternate"],
});
const customType3GetterSync = key => ({
key,
value: key + "_value_custom",
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("key2")).toEqual("key2_value");
expect(cache.get("key2_alternate")).toEqual("key2_value");
expect(cache.get("key2", false, customType3GetterSync)).toEqual("key2_value");
expect(cache.get("key2_alternate", false, customType3GetterSync)).toEqual("key2_value");
expect(cache.get("key2", true, customType3GetterSync)).toEqual("key2_value_custom");
expect(cache.get("key2")).toEqual("key2_value_custom");
expect(cache.get("key2_alternate")).toEqual("key2_value_custom");
cache.setEntryGetter(null);
cache.clear();
});
});

@@ -419,0 +452,0 @@

@@ -0,0 +0,0 @@ /* eslint-disable no-magic-number */

@@ -0,0 +0,0 @@ /* eslint-disable no-magic-number, no-unused-expressions */

@@ -0,0 +0,0 @@ const path = require("path");

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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