Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mcollina/single-user-cache
https://github.com/mcollina/single-user-cache
Last synced: 3 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/mcollina/single-user-cache
- Owner: mcollina
- License: other
- Created: 2019-03-08T14:59:23.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2023-11-16T16:38:47.000Z (about 1 year ago)
- Last Synced: 2024-12-17T15:42:14.711Z (5 days ago)
- Language: JavaScript
- Size: 21.5 KB
- Stars: 37
- Watchers: 3
- Forks: 5
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# single-user-cache
A simple cache system for a single user request, built on the same concepts of [data loader](https://github.com/facebook/dataloader).
## Install
```js
npm i single-user-cache
```## Usage
```js
const { Factory } = require('.')
const factory = new Factory()factory.add('fetchSomething', {
// cache by default, set to false to just do batching
cache: true
}, async (queries, context) => {
console.log(queries)
// [ 42, 24 ]console.log(context)
// { some: 'data' }return queries.map((k) => {
return { k }
})
})async function run () {
const context = {
some: 'data'
}
const cache = factory.create(context)const p1 = cache.fetchSomething(42)
const p2 = cache.fetchSomething(24)const res = await Promise.all([p1, p2])
console.log(res)
// [
// { k: 42 },
// { k: 24 }
// ]
}run().catch(console.log)
```If the query parameter is an object, its cache key will be generated
using
[safe-stable-stringify](https://github.com/BridgeAR/safe-stable-stringify).## License
MIT