{"_id":"@aftonbladet/loading-cache","_rev":"8-486dbcc2d79b8a096db9ee6600bf7fea","name":"@aftonbladet/loading-cache","description":"A loading cache implementation with possibility to set a maximum size and timeout entries","dist-tags":{"latest":"1.0.0"},"versions":{"0.1.0":{"name":"@aftonbladet/loading-cache","version":"0.1.0","author":{"name":"Patrik Åkerstrand","email":"patrik.akerstrand@schibsted.se"},"main":"lib/index.js","description":"A loading cache implementation with possibility to set a maximum size and timeout entries","keywords":["cache","loading cache","max size","entry expiration","promise based"],"engines":{"node":">=4.0.0"},"scripts":{"test":"NODE_ENV=test mocha --compilers js:babel/register --recursive","lint":"eslint src","compile":"babel src --out-dir lib","prepublish":"npm run compile"},"repository":{"type":"git","url":"git@github.schibsted.io:ab/js-loading-cache.git"},"license":"UNLICENSED","dependencies":{"babel":"^5.8.23","debug":"^2.2.0"},"devDependencies":{"babel-eslint":"^4.1.3","chai":"^3.3.0","eslint":"^1.7.3","eslint-config-airbnb":"^0.1.0","mocha":"^2.3.3","sinon":"^1.17.2"},"gitHead":"a5a15c1e22ec43c24c377bc13ca20437fae1dbd2","_id":"@aftonbladet/loading-cache@0.1.0","_shasum":"793e195e6d5c180b80a7d79e89c1573bba50cb30","_from":".","_npmVersion":"3.3.6","_nodeVersion":"4.2.1","_npmUser":{"name":"aftonbladet","email":"tobias.jarlund@aftonbladet.se"},"dist":{"shasum":"793e195e6d5c180b80a7d79e89c1573bba50cb30","tarball":"https://registry.npmjs.org/@aftonbladet/loading-cache/-/loading-cache-0.1.0.tgz","integrity":"sha512-cQ874Ojgt5inyyU99pb5KSSBmUKbxNf8KXGLpwYu96oZ+imoX3nnfJRde5y9TUlV9Y6cGW67qfpil99uYGuFSQ==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDRhmtaZTo3jHNx251wYPW7YIfqTVX7FqMwun8ysT+oagIgORZ5CW/32bK+r31OommnSeQHh9gxK0Yo3d9WcSj9lB0="}]},"maintainers":[{"name":"aftonbladet","email":"tobias.jarlund@aftonbladet.se"}]},"0.2.0":{"name":"@aftonbladet/loading-cache","version":"0.2.0","author":{"name":"Patrik Åkerstrand","email":"patrik.akerstrand@schibsted.se"},"main":"lib/index.js","description":"A loading cache implementation with possibility to set a maximum size and timeout entries","keywords":["cache","loading cache","max size","entry expiration","promise based"],"engines":{"node":">=4.0.0"},"scripts":{"test":"NODE_ENV=test mocha --compilers js:babel/register --recursive","lint":"eslint src","clean":"rimraf lib dist coverage","compile":"babel src --out-dir lib","prepublish":"npm run clean && npm run compile","preversion":"npm test","version":"npm publish","postversion":"git push && git push --tags"},"repository":{"type":"git","url":"git@github.schibsted.io:ab/js-loading-cache.git"},"license":"UNLICENSED","dependencies":{"babel":"^5.8.23","debug":"^2.2.0"},"devDependencies":{"babel-eslint":"^4.1.3","chai":"^3.3.0","eslint":"^1.7.3","eslint-config-airbnb":"^0.1.0","mocha":"^2.3.3","rimraf":"^2.4.3","sinon":"^1.17.2"},"gitHead":"b65df08f99f590ba9aae585cda797e84d50e9ef2","_id":"@aftonbladet/loading-cache@0.2.0","_shasum":"808a6d81e35bc82d77736a6692d7aed922d01d5a","_from":".","_npmVersion":"3.3.6","_nodeVersion":"4.2.1","_npmUser":{"name":"aftonbladet","email":"tobias.jarlund@aftonbladet.se"},"dist":{"shasum":"808a6d81e35bc82d77736a6692d7aed922d01d5a","tarball":"https://registry.npmjs.org/@aftonbladet/loading-cache/-/loading-cache-0.2.0.tgz","integrity":"sha512-kDpdrWwgae1uNsBvSLSvqJOt4qnvFwvte8rUVc1qFHf2lk5btCBCW/Fva4PK+9Ha8Jb/pX6XhKdqTiEKMeGytQ==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDkYS7S8QzP1Ej8z3B23PcY5C1x1a960KCwBLLWOjlRsAIgVecO6w2pRFjJdUbyYKua8efxNl11AGvzm1VQTzK/fqU="}]},"maintainers":[{"name":"aftonbladet","email":"tobias.jarlund@aftonbladet.se"}]},"0.3.0":{"name":"@aftonbladet/loading-cache","version":"0.3.0","author":{"name":"Patrik Åkerstrand","email":"patrik.akerstrand@schibsted.se"},"main":"lib/index.js","description":"A loading cache implementation with possibility to set a maximum size and timeout entries","keywords":["cache","loading cache","max size","entry expiration","promise based"],"engines":{"node":">=4.0.0"},"scripts":{"test":"NODE_ENV=test mocha --compilers js:babel-register --recursive","lint":"eslint src","clean":"rimraf lib dist coverage","compile":"babel src --out-dir lib","prepublish":"npm run clean && npm run compile","preversion":"npm test","version":"npm publish","postversion":"git push && git push --tags"},"repository":{"type":"git","url":"git@github.schibsted.io:ab/js-loading-cache.git"},"license":"UNLICENSED","dependencies":{"debug":"^2.2.0"},"devDependencies":{"babel-cli":"^6.0.0","babel-core":"^6.0.0","babel-eslint":"^6.0.4","babel-preset-es2015":"^6.6.0","babel-register":"^6.7.2","chai":"^3.3.0","eslint":"^2.0.0","eslint-config-airbnb-base":"^v1.0.0","eslint-plugin-import":"^1.6.1","mocha":"^2.3.3","rimraf":"^2.4.3","sinon":"^1.17.2"},"gitHead":"e7569ec43b98935ef8dec600236a436b136fba6a","_id":"@aftonbladet/loading-cache@0.3.0","_shasum":"c9a7b00b5539d2c6bdea68f7c6dc17279b0dbe92","_from":".","_npmVersion":"3.8.6","_nodeVersion":"4.2.6","_npmUser":{"name":"aftonbladet","email":"tobias.jarlund@aftonbladet.se"},"dist":{"shasum":"c9a7b00b5539d2c6bdea68f7c6dc17279b0dbe92","tarball":"https://registry.npmjs.org/@aftonbladet/loading-cache/-/loading-cache-0.3.0.tgz","integrity":"sha512-bJuIAWIksBj25tuCPE9MzltWFSOAsZGcHKNRJDQFuB1I9Vv8iPQ1k1GWyLelTInawaQBk+h24zuCULdU3cjBcg==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDLZxcWmHhcizjnsR/R3bocT2ksjjiBVedYMDILgVyVHwIhANuHrXO0vHVmKLKl48CfrGBEBvdYy0bzkPNB5by0TNec"}]},"maintainers":[{"name":"aftonbladet","email":"tobias.jarlund@aftonbladet.se"}],"_npmOperationalInternal":{"host":"packages-12-west.internal.npmjs.com","tmp":"tmp/loading-cache-0.3.0.tgz_1461917124722_0.1184747046791017"}},"0.4.0":{"name":"@aftonbladet/loading-cache","version":"0.4.0","author":{"name":"Patrik Åkerstrand","email":"patrik.akerstrand@schibsted.se"},"main":"lib/index.js","description":"A loading cache implementation with possibility to set a maximum size and timeout entries","keywords":["cache","loading cache","max size","entry expiration","promise based"],"engines":{"node":">=4.0.0"},"scripts":{"test":"NODE_ENV=test mocha --compilers js:babel-register --recursive","lint":"eslint src","clean":"rimraf lib dist coverage","compile":"babel src --out-dir lib","prepublish":"npm run clean && npm run compile","preversion":"npm test","version":"npm publish","postversion":"git push && git push --tags"},"repository":{"type":"git","url":"git@github.schibsted.io:ab/js-loading-cache.git"},"license":"UNLICENSED","dependencies":{"debug":"^2.2.0"},"devDependencies":{"babel-cli":"^6.0.0","babel-core":"^6.0.0","babel-eslint":"^6.0.4","babel-preset-es2015":"^6.6.0","babel-register":"^6.7.2","chai":"^3.3.0","chai-as-promised":"^5.2.0","eslint":"^2.0.0","eslint-config-airbnb-base":"^v1.0.0","eslint-plugin-import":"^1.6.1","mocha":"^2.3.3","rimraf":"^2.4.3","sinon":"^1.17.2"},"gitHead":"6ee261d57dd38b984713a8534c5150bd3a4948f4","_id":"@aftonbladet/loading-cache@0.4.0","_shasum":"e7a8ffddae69f891eb49bac1cb6dced5c3edf008","_from":".","_npmVersion":"3.10.3","_nodeVersion":"6.6.0","_npmUser":{"name":"aftonbladet","email":"tobias.jarlund@aftonbladet.se"},"dist":{"shasum":"e7a8ffddae69f891eb49bac1cb6dced5c3edf008","tarball":"https://registry.npmjs.org/@aftonbladet/loading-cache/-/loading-cache-0.4.0.tgz","integrity":"sha512-VlEwKpvhspqe5p/xZKGN/7FeJZF4VLNBJAJVc4M/X2gQHW57Hcvh3jYixRSFnL7boc3JfFZzwVut3d+GvXSdrw==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIG+QZIcmWS4zkw5bnnix4FO/9BU8gqpggv4iRzMZLVmUAiAHlbAe5aB8g14PYMrz13Ms3Ub5BvHFGkZJloDlqDseTg=="}]},"maintainers":[{"name":"aftonbladet","email":"tobias.jarlund@aftonbladet.se"}],"_npmOperationalInternal":{"host":"packages-18-east.internal.npmjs.com","tmp":"tmp/loading-cache-0.4.0.tgz_1481644277025_0.03586264164187014"}},"1.0.0":{"name":"@aftonbladet/loading-cache","version":"1.0.0","author":{"name":"Patrik Åkerstrand","email":"patrik.akerstrand@schibsted.se"},"main":"lib/index.js","description":"A loading cache implementation with possibility to set a maximum size and timeout entries","keywords":["cache","loading cache","max size","entry expiration","promise based"],"engines":{"node":">=4.0.0"},"scripts":{"test":"NODE_ENV=test mocha --compilers js:babel-register --recursive","lint":"eslint src","clean":"rimraf lib dist coverage","compile":"babel src --out-dir lib","prepublish":"npm run clean && npm run compile","preversion":"npm test","version":"npm publish","postversion":"git push && git push --tags"},"repository":{"type":"git","url":"git@github.schibsted.io:ab/js-loading-cache.git"},"license":"UNLICENSED","dependencies":{"debug":"^2.2.0"},"devDependencies":{"babel-cli":"^6.0.0","babel-core":"^6.0.0","babel-eslint":"^6.0.4","babel-preset-es2015":"^6.6.0","babel-register":"^6.7.2","chai":"^3.3.0","chai-as-promised":"^5.2.0","eslint":"^2.0.0","eslint-config-airbnb-base":"^v1.0.0","eslint-plugin-import":"^1.6.1","mocha":"^2.3.3","rimraf":"^2.4.3","sinon":"^1.17.2"},"gitHead":"cbbd070baf24e8c7ec7be51335a4de47d48655c6","_id":"@aftonbladet/loading-cache@1.0.0","_npmVersion":"5.5.1","_nodeVersion":"8.6.0","_npmUser":{"name":"aftonbladet","email":"tobias.jarlund@aftonbladet.se"},"dist":{"integrity":"sha512-1uKlxkb6wsOdMVSCC3XWVOdYirAHP4Wd4IQTTH9nH22I5ptxFjUd4cIBDGKFDLs5h9kl2GQiBBmW/2ISJOFjYg==","shasum":"24990eec8979623da38e299ab0c8eb9706fc4116","tarball":"https://registry.npmjs.org/@aftonbladet/loading-cache/-/loading-cache-1.0.0.tgz","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIAtwYYs68o52CGyotyYsjZYpJA5cFxPmKlCt1RP7bakQAiBI4YrZoKHhTEn3yhCEwPmZuOb3GAdrfsRGYzBZt1JBeQ=="}]},"maintainers":[{"name":"aftonbladet","email":"tobias.jarlund@aftonbladet.se"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/loading-cache-1.0.0.tgz_1509031215744_0.913411722285673"}}},"readme":"# js-loading-cache [![build status](https://travis.schibsted.io/ab/js-loading-cache.svg?token=MjZ6BGzPrzsS6ytu4EvJ&branch=master)](https://travis.schibsted.io/ab/js-loading-cache)\nA loading cache implementation with possibility to set a maximum size and timeout entries\n\n## Usage\n```js\nimport LoadingCache from '@aftonbladet/loading-cache';\nimport request from 'co-request';\n\nconst cache = new LoadingCache(url => request(url), {\n  maxAge: 1000 * 60 * 5,\n  maxEntries: 1000\n});\n\ncache.get('http://example.com')\n  .then(result => {\n  });\n```\n\n## API\n### Constructor\n#### `constructor([options])`\nCreates a new LoadingCache instance\n\n* `{object} options`: A config object to configure how the cache behaves:\n    * `{number} maxEntries`: The maximum total amount of entries in the cache. See [Eviction](#eviction) for how entries are removed from the cache.\n    * `{number} maxAge`: The maximum time (in milliseconds) an entry remains in the cache. If provided it should be a number larger than 0.\n    * `{number} freshAge`: The amount of time in milliseconds entries are considered to be fresh. See [Serving stale entries](#serving-stale-entries) for details.\n    * `{Function} defaultLoader`: This function is called with the key to populate if the value is not\n      found in the cache. The function must return a value synchronously, but it is ok to return a `Promise` if works needs to be done async. \n\n### Methods\n#### `get(key, [loaderFn])`\nFetches an item from the cache. If `key` is not already in the cache then `loaderFn`, provided in the constructor, is\ninvoked and the cache populated for that `key`.\n\nGet always returns a `Promise` for a value, to allow the loader function to work asynchronously.\n\n* `{*} key`: Any value that is a valid key for a `Map` works as a key in the `LoadingCache`.\n* `{Function} [loaderFn]`: Optional if `defaultLoader` was specified when constructing the cache instance.\n   This function is called with the key to populate if the value is not\n   found in the cache. The function must return a value synchronously, but it is ok to return a `Promise` if works needs to be done async.\n   \n* **Returns**: `Promise{*}` - The value returned by `loaderFn` wrapped in a `Promise`.\n\n#### `has(key)`\nChecks if a key has an entry in the cache. Does _NOT_ invoke the loader function if the\nkey is not in th cache.\n\n* `{*} key`: The key to check in the cache\n* **Returns**: `{boolean}` - `true` if `key` is in the cache, `false` otherwise\n\n#### `clear()`\nEmpties the cache\n\n### Properties\n#### `{number} size`\nThe current number of entries in the cache\n\n#### `{number|undefined} maxEntries`\nThe maximum amount of entries allowed in the cache\n\n#### `{number|undefined} maxAge`\nThe maximum amount of time entries are allowed to stay in the cache in milliseconds.\n\n#### `{number|undefined} freshAge`\nThe amount of time in milliseconds entries are considered to be fresh. Entries that are older than `freshAge` will\ncontinue to be served (as long as they are not evicted by `maxAge`), but the `loaderFn` will trigger an update in\nthe background.\n**Note** `maxAge` need to be larger than `freshAge` for this to have any effect.\n\n## Cache eviction\nThe default cache eviction strategy is based upon an LRU-scheme. This means that \"hot\" items that are fetched often\nhave a higher chance to remain in the cache, while entries that are fetched only a few times will eventually be\nevicted from the cache.\n\n**NOTE:** You must provide a value for `maxEntries` for this to apply.\n\nAdditionally, entries can have a `maxAge`. Cache entries that have expired are evicted before items are evicted due\nto a cache size exceeding `maxEntries.\n\nIf you are not happy with the default eviction implementation, **you can provide your own eviction strategy** to the\nconstructor. The cache expects to receive an object (or constructor function) with the following interface:\n\n## Serving stale entries\nIf both the `maxAge` and `freshAge` properties are set, the cache will be able to serve stale entries in the time\nperiod after `freshAge` but before `maxAge`. The stale content will be served during the `loaderFn` is busy updating\nthe cache entry or even if an error occurs in the `loaderFn`.\n\nThis can be used to make sure that you always return a cached entry quickly and let the update continue in the\nbackground. It is also useful to be able to return stale content during a restart of a backing service or maybe\na network glitch or similar.\n\n### Cache eviction API\n\n#### `constructor({maxEntries, maxAge, freshAge})`\nConstructs a new eviction strategy based upon the provided constructor function. You can either pass in the \nconstructor function, in which case you'll receive `maxEntries`, `maxAge` and `freshAge` as an config object argument,\nor you can construct the eviction API yourself and provide that object to the constructor of the `LoadingCache`.\n\n#### `onLoad(key, val, entries)`\nCalled after the cache received a value from the `loaderFn`. Note that `entries` has already been populated with\n`val` at this point.\n\n* `{*} key`: The key of `val`, which is the loaded value.\n* `{Promise<*>} val`: A `Promise` for the eventual value of `val`.\n* `{Map} entries`: All the entries in the cache, including `val`.\n\n#### `onGet(key, val, entries)`\nCalled after `key` was found in the cache. Here you can perform management tasks such as keeping track of which\nkeys are fetched etc.\n\n* `{*} key`: The key that was fetched\n* `{Promise<*>} val`: A `Promise` for the eventual value of `val` corresponding to `key`.\n* `{Map} entries`: All the entries in the cache.\n\n#### `isValid(key, val, entries)`\nCalled when `key` has an entry in the cache, but before `val` is returned to the caller.\n\n* `{*} key`: The key that was fetched\n* `{Promise<*>} val`: A `Promise` for the eventual value of `val` corresponding to `key`.\n* `{Map} entries`: All the entries in the cache.\n* **Returns**: `{boolean}` - `true` if `val` is a valid entry for `key`, `false` otherwise.\n","maintainers":[{"name":"aftonbladet","email":"tobias.jarlund@aftonbladet.se"}],"time":{"modified":"2022-06-12T14:21:15.440Z","created":"2015-10-31T23:25:10.886Z","0.1.0":"2015-10-31T23:25:10.886Z","0.2.0":"2015-11-05T14:57:27.957Z","0.3.0":"2016-04-29T08:05:26.780Z","0.4.0":"2016-12-13T15:51:17.643Z","1.0.0":"2017-10-26T15:20:16.776Z"},"keywords":["cache","loading cache","max size","entry expiration","promise based"],"repository":{"type":"git","url":"git@github.schibsted.io:ab/js-loading-cache.git"},"author":{"name":"Patrik Åkerstrand","email":"patrik.akerstrand@schibsted.se"},"license":"UNLICENSED","readmeFilename":"README.md"}