Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kaelzhang/p-async-cache
Cache the async promise lookups and avoid fetching the same thing more than necessary.
https://github.com/kaelzhang/p-async-cache
async cache lru-cache nodejs promise
Last synced: 20 days ago
JSON representation
Cache the async promise lookups and avoid fetching the same thing more than necessary.
- Host: GitHub
- URL: https://github.com/kaelzhang/p-async-cache
- Owner: kaelzhang
- License: other
- Created: 2017-03-08T01:53:29.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2017-11-27T06:11:04.000Z (almost 7 years ago)
- Last Synced: 2024-05-22T04:02:54.213Z (6 months ago)
- Topics: async, cache, lru-cache, nodejs, promise
- Language: JavaScript
- Homepage:
- Size: 7.81 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: HISTORY.md
- License: LICENSE-MIT
Awesome Lists containing this project
README
[![Build Status](https://travis-ci.org/kaelzhang/p-async-cache.svg?branch=master)](https://travis-ci.org/kaelzhang/p-async-cache)
# p-async-cache
Cache the promise lookups and avoid fetching the same thing more than necessary.
## Install
```sh
$ npm install p-async-cache --save
```## Usage
```js
import PAC from 'p-async-cache'let counter = 0
const cache = new PAC({
async load (userId) {
counter ++
return await getUserFromRemote(userId)
}
})function get () {
cache.get(123).then(({value}) => {
console.log(value, counter)
})
}get()
get()// [object User] 1
// [object User] 1 (The counter still be 1)
```## new AC(options)
- **options** `Object=`
- stale `Boolean` whether allow stale value
- stringify `function()=JSON.stringify` method to serialize the `params` to a cache key.
- load `AsyncFunction(...params)|function(...params):Promise|function(...params)` accepts a normal synchronous function, a function that returns a promise, or an async function, and the `params` will be the parameters of the `.get(...params)` method.
- other options that [lru-cache](https://www.npmjs.com/package/lru-cache) supports### Example for `options.stale`
```js
import delay from 'delay'const cache = new AC({
stale: true,
maxAge: 100,
load (a, b) {
return delay(1).then(() => a + b)
}
})cache.get(1, 2)
.then(({value, stale}) => {
console.log(value) // 3
console.log(stale) // false// Delay a timespan which is bigger than `maxAge`
return delay(101).then(() => cache.get(1, 2))
})
.then(({value, stale}) => {
console.log(value) // 3
console.log(stale) // true (the value is stale)return delay(10).then(() => cache.get(1, 2))
})
.then(({value, stale}) => {
console.log(value) // 3
console.log(stale) // false (if the value is found as stale, it will refresh the value in the background)
})
```## .get(...params)
- **params** `any` that will be passed into `options.load(...params)`
Returns `Promise`
Lookup the value in the cache,
- if found, then return.
- if not found,
- if allow stale values, and the value is stale, then return the value, and refresh value in background.
- otherwise, load the value with `load(...params)`## Other methods of `lru-cache`
- `.reset()`
- `.has(...params)`
- `.peek(...params)`
- `.del(...params)`## License
MIT