@5app/memoize
Advanced tools
Comparing version 1.1.0 to 1.2.0
@@ -0,1 +1,8 @@ | ||
# [1.2.0](https://github.com/5app/memoize/compare/v1.1.0...v1.2.0) (2020-07-30) | ||
### Features | ||
* **cacheMaxSize:** Remove oldest items from cache ([a92d21d](https://github.com/5app/memoize/commit/a92d21d260e5654a40c609a94a9a79424cd4a838)) | ||
# [1.1.0](https://github.com/5app/memoize/compare/v1.0.2...v1.1.0) (2020-02-21) | ||
@@ -2,0 +9,0 @@ |
15
index.js
@@ -20,2 +20,3 @@ /** | ||
* @param {object} [opts.cache=new Map()] - Caching function uses Map by default | ||
* @param {number} [opts.cacheMaxSize=1000] - Maximum Cache Size | ||
* @returns {Function} The decorated callback function | ||
@@ -36,2 +37,5 @@ */ | ||
cache = new Map(), | ||
// cache Max Size | ||
cacheMaxSize = 1000, | ||
} = opts; | ||
@@ -41,3 +45,3 @@ | ||
// If we have a resolved value, but we want to keep it up to date set to true | ||
let shouldUseCache = item => item.status === 'resolved' && useCached; | ||
let shouldUseCache = (item) => item.status === 'resolved' && useCached; | ||
@@ -121,2 +125,11 @@ // If the settings say it's a function use that instead | ||
// Does the cache need a trim? | ||
if (cacheMaxSize && cache.size > cacheMaxSize) { | ||
for (const k of cache.keys()) { | ||
// Remove the first key and break | ||
cache.delete(k); | ||
break; | ||
} | ||
} | ||
// Return the item value | ||
@@ -123,0 +136,0 @@ return item.value; |
{ | ||
"name": "@5app/memoize", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Memoize decorator", | ||
@@ -28,18 +28,18 @@ "main": "index.js", | ||
"devDependencies": { | ||
"@commitlint/cli": "^8.2.0", | ||
"@commitlint/config-conventional": "^8.2.0", | ||
"@semantic-release/changelog": "^5.0.0", | ||
"@commitlint/cli": "^9.0.0", | ||
"@commitlint/config-conventional": "^9.0.0", | ||
"@semantic-release/changelog": "^5.0.1", | ||
"@semantic-release/git": "^9.0.0", | ||
"chai": "^4.2.0", | ||
"chai-as-promised": "^7.1.1", | ||
"eslint": "^6.7.2", | ||
"eslint-config-5app": "^0.8.0", | ||
"eslint-config-prettier": "^6.7.0", | ||
"eslint-plugin-prettier": "^3.1.1", | ||
"husky": "^4.0.0", | ||
"mocha": "^7.0.1", | ||
"nyc": "^15.0.0", | ||
"prettier": "^1.19.1", | ||
"eslint": "^7.0.0", | ||
"eslint-config-5app": "^0.12.0", | ||
"eslint-config-prettier": "^6.11.0", | ||
"eslint-plugin-prettier": "^3.1.3", | ||
"husky": "^4.2.5", | ||
"mocha": "^8.0.0", | ||
"nyc": "^15.1.0", | ||
"prettier": "^2.0.0", | ||
"pretty-quick": "^2.0.1", | ||
"semantic-release": "^17.0.0" | ||
"semantic-release": "^17.0.8" | ||
}, | ||
@@ -46,0 +46,0 @@ "publishConfig": { |
# Memoize | ||
[![CircleCI](https://circleci.com/gh/5app/memoize.svg?style=shield)](https://circleci.com/gh/5app/memoize) | ||
@@ -16,3 +17,3 @@ | ||
// Let's say we're going to decorate an add function... it's | ||
// Let's say we're going to decorate an add function... it's | ||
const memoGot = memoize(got); | ||
@@ -27,2 +28,43 @@ | ||
# Options `memoize(handler, {...options})` | ||
- `option.useCache` _(Boolean|Function)_: A truthy/fasly or a function to decide whether to use the cached record or not. Default `true` | ||
- `option.staleInMs` _Number_: The number of milliseconds before the cache is deemed stale. Results will still be served from the cache whilst an attempt to refresh the cache is made separatly. Default `10000` ms. | ||
- `option.getKey` _Function_: A function to create a key based upon the input of the function being memoized. Default: a serialization of all the arguments. | ||
- `option.cache` _Object_: Instance of a Map like object to store the cache. Default `new Map` | ||
- `options.cacheMaxSize` _Number_: The maximum number of entries to store in the cache. Default `1000` | ||
## `option.useCache` | ||
Whether to use cache this can be a Boolean value (useful to disable it when testing). Or a function e.g. | ||
This snippet checks the cached value before deciding whether to use it... | ||
```js | ||
const memoize = require('@5app/memoize'); | ||
const memoGot = memoize(got, { | ||
/** | ||
* @param {object} cached_response - Cached Object | ||
* @param {number} cached_response.timestamp - Timestamp when request resolved | ||
* @param {string} cached_response.status - 'pending', 'fullfilled', 'rejected' | ||
* @param {Promise<*>} cached_response.value - Promise of the request | ||
* @returns {Boolean} | ||
*/ | ||
useCache({timestamp, status}) { | ||
// Set an expiry on the cache. | ||
// 2xx, 3xx response last for a full minute before being reused | ||
// 4xx, 5xx last only a second... | ||
const age = value.statusCode >= 400 ? 1000 : 60000; | ||
// Return true if the cache is un-expired, else false. | ||
return timestamp > Date.now() - AGE; | ||
} | ||
} | ||
// ... | ||
// Use Memogot | ||
// const req = memogot('link'); | ||
// ... | ||
``` |
10765
116
69