https://github.com/75lb/cache-point
Simple, filesystem-backed memoisation cache.
https://github.com/75lb/cache-point
cache javascript javascript-library memoization nodejs
Last synced: 9 months ago
JSON representation
Simple, filesystem-backed memoisation cache.
- Host: GitHub
- URL: https://github.com/75lb/cache-point
- Owner: 75lb
- License: mit
- Created: 2016-07-23T22:11:52.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2024-12-06T00:06:30.000Z (over 1 year ago)
- Last Synced: 2025-03-18T02:51:20.032Z (over 1 year ago)
- Topics: cache, javascript, javascript-library, memoization, nodejs
- Language: JavaScript
- Homepage:
- Size: 78.1 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.hbs
- License: LICENSE
Awesome Lists containing this project
README
[](https://www.npmjs.org/package/cache-point)
[](https://www.npmjs.org/package/cache-point)
[](https://github.com/75lb/cache-point/network/dependents?dependent_type=REPOSITORY)
[](https://github.com/75lb/cache-point/network/dependents?dependent_type=PACKAGE)
[](https://github.com/75lb/cache-point/actions/workflows/node.js.yml)
[](https://github.com/feross/standard)
# cache-point
Simple, filesystem-backed memoisation cache. Use to cache the output of expensive operations speeding up future invocations with the same input.
## Synopsis
```js
import Cache from 'cache-point'
import { setTimeout as sleep } from 'node:timers/promises'
/* mock function to simulate a slow, remote request */
async function fetchUser (id) {
await sleep(1000)
return { id, name: 'Layla' }
}
class Users {
constructor () {
this.cache = new Cache({ dir: 'tmp/example' })
}
async getUser (id) {
let user
try {
/* cache.read() will resolve on hit, reject on miss */
user = await this.cache.read(id)
} catch (err) {
if (err.code === 'ENOENT') {
/* cache miss, fetch remote user */
user = await fetchUser(id)
this.cache.write(id, user)
}
}
return user
}
}
console.time('getUser')
const users = new Users()
const user = await users.getUser(10)
console.timeEnd('getUser')
console.log(user)
```
The first invocation will take 1 second while the remote user is fetched.
```
$ node example/simple.js
getUser: 1.025s
{ id: 10, name: 'Layla' }
```
Since the cache is now warm, future invocations will be fast.
```
$ node example/simple.js
getUser: 17.07ms
{ id: 10, name: 'Layla' }
```
## API Reference
{{>main}}
* * *
© 2016-25 Lloyd Brookes \.
Tested by [test-runner](https://github.com/test-runner-js/test-runner). Documented by [jsdoc-to-markdown](https://github.com/jsdoc2md/jsdoc-to-markdown).