https://github.com/fabsrc/re-mem
🧠Memoization with stale-while-revalidate and stale-if-error
https://github.com/fabsrc/re-mem
hacktoberfest memoization swr
Last synced: 4 days ago
JSON representation
🧠Memoization with stale-while-revalidate and stale-if-error
- Host: GitHub
- URL: https://github.com/fabsrc/re-mem
- Owner: fabsrc
- License: mit
- Created: 2020-10-12T19:29:46.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2023-10-19T10:43:57.000Z (over 2 years ago)
- Last Synced: 2025-03-01T00:12:12.459Z (over 1 year ago)
- Topics: hacktoberfest, memoization, swr
- Language: TypeScript
- Homepage:
- Size: 172 KB
- Stars: 1
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# re-mem
> Fork of [mem](https://github.com/sindresorhus/mem/) with "staleWhileRevalidate" and "staleIfError"

## Install
```shell
npm install -S @fabsrc/re-mem
```
## Usage
```js
import reMem from "@fabsrc/re-mem"
function getData(id) {
return Promise.resolve(`Data: ${id}`)
}
const getDataMemoized = reMem(getData, {
maxAge: 1000,
staleWhileRevalidate: 10000,
staleIfError: 20000
})
getDataMemoized(123)
.then(console.log)
.catch(console.error)
```
### Options
- `cacheKey` Function that returns a cache key based on arguments passed to a function. By default the first argument is used as cache key
- `cache` Custom cache to store data in. (Default: `new Map()`)
- `cachePromiseRejection` Boolean flag wether to cache rejected Promises or not (Default: `false`)
- `maxAge` Time in ms to return the cached promise (Default: `Infinity`)
- `staleWhileRevalidate` Time in ms to return stale data while revalidating the data in the background. The time starts after `maxAge` runs out.
- `staleIfError` Time in ms to return stale data if original promise rejects with an error.
## Development
### Testing
```shell
npm test
```