https://github.com/microlinkhq/memoize-token
A memoize library for refreshing and cache token values.
https://github.com/microlinkhq/memoize-token
Last synced: about 1 year ago
JSON representation
A memoize library for refreshing and cache token values.
- Host: GitHub
- URL: https://github.com/microlinkhq/memoize-token
- Owner: microlinkhq
- License: mit
- Created: 2018-09-19T16:45:00.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2024-12-07T15:38:45.000Z (over 1 year ago)
- Last Synced: 2025-04-13T08:17:32.676Z (about 1 year ago)
- Language: JavaScript
- Size: 68.4 KB
- Stars: 2
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README

[](https://www.npmjs.org/package/memoize-token)
> A memoize library for refreshing and cache token values.
## Install
```bash
$ npm install memoize-token --save
```
## Usage
```js
const memoizeToken = require('memoize-token')
let value = -1
const values = ['foo', 'bar']
const fn = memoizeToken(
() => values[++value],
{ max: 2, key: 'test', cache }
)
;(async () => {
await fn() // => 'foo'
await fn() // => 'foo'
await fn() // max excedeed, refresh the token => 'bar'
await fn() // => 'bar'
})()
```
## API
### memoizeToken(fn, [options])
#### fn
*Required*
Type: `Function`
Function to be memoized and used for refreshing the token.
#### options
##### max
*Required*
Type: `Number`
Maximum numbers of calls after refresh the token.
##### key
*Required*
Type: `String`
The base key to use into the cache.
##### expire
Type: `Number`
Time in milliseconds to consider a token as expired.
##### cache
Type: `Object`
Default: `new Map()`
Use a different cache storage.
Must implement the following methods: `.get(key)` and `.set(key, value)`, Check [`keyv`](https://github.com/lukechilds/keyv) to see database connectors.
## Related
- [bottleneck](https://github.com/SGrondin/bottleneck) – Job scheduler and rate limiter.
- [limiter](https://github.com/jhurliman/node-rate-limiter) – Provides a generic rate limiter and token bucket for Node.js.
## License
**memoize-token** © [microlink.io](https://microlink.io), released under the [MIT](https://github.com/microlinkhq/memoize-token/blob/master/LICENSE.md) License.
Authored and maintained by [Kiko Beats](https://kikobeats.com) with help from [contributors](https://github.com/microlinkhq/memoize-token/contributors).
> [microlink.io](https://microlink.io) · GitHub [microlink.io](https://github.com/microlinkhq) · Twitter [@microlinkhq](https://twitter.com/microlinkhq)