@digitalbazaar/lru-memoize
Advanced tools
Comparing version 2.1.0 to 2.2.0
# @digitalbazaar/lru-memoize ChangeLog | ||
## 2.2.0 - 2022-02-27 | ||
### Added | ||
- Allow cache `options` to be passed to `memoize`. Only cache options | ||
that are supported by the underlying LRU instance will be used. | ||
## 2.1.0 - 2021-06-30 | ||
@@ -4,0 +10,0 @@ |
@@ -26,4 +26,4 @@ /*! | ||
export class LruCache { | ||
constructor(cacheOptions) { | ||
this.options = cacheOptions || {}; | ||
constructor(cacheOptions = {}) { | ||
this.options = cacheOptions; | ||
this.cache = new LRU(cacheOptions); | ||
@@ -50,6 +50,9 @@ } | ||
* promise to memoize. | ||
* @param {object} options.options - The LRU cache options to pass | ||
* if setting the value in the cache; note: only those options supported | ||
* by the underlying LRU cache instance will be supported. | ||
* | ||
* @returns {Promise} - The result of the memoized promise. | ||
*/ | ||
async memoize({key, fn}) { | ||
async memoize({key, fn, options = {}} = {}) { | ||
let promise = this.cache.get(key); | ||
@@ -61,4 +64,8 @@ if(promise) { | ||
// cache miss | ||
const cacheOptions = {...this.options, ...options}; | ||
promise = fn(); | ||
this.cache.set(key, promise); | ||
// this version only supports `maxAge` and `disposeOnSettle`; a future | ||
// version will support more cache options | ||
const {maxAge} = cacheOptions; | ||
this.cache.set(key, promise, maxAge); | ||
@@ -74,3 +81,3 @@ let result; | ||
if(this.options.disposeOnSettle) { | ||
if(cacheOptions.disposeOnSettle) { | ||
this.cache.del(key); | ||
@@ -77,0 +84,0 @@ } |
{ | ||
"name": "@digitalbazaar/lru-memoize", | ||
"version": "2.1.0", | ||
"version": "2.2.0", | ||
"description": "LRU Memoize", | ||
@@ -5,0 +5,0 @@ "license": "BSD-3-Clause", |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
6851
88