[![view on npm](http://img.shields.io/npm/v/cache-point.svg)](https://www.npmjs.org/package/cache-point)
[![npm module downloads](http://img.shields.io/npm/dt/cache-point.svg)](https://www.npmjs.org/package/cache-point)
[![Build Status](https://travis-ci.org/75lb/cache-point.svg?branch=master)](https://travis-ci.org/75lb/cache-point)
[![Dependency Status](https://badgen.net/david/dep/75lb/cache-point)](https://david-dm.org/75lb/cache-point)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](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
const Cache = require('cache-point')

/* a mock function to simulate a slow remote request */
async function fetchUser (id) {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve({ id, name: 'Layla' })
    }, 1000)
  })
}

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
  }
}

async function start () {
  console.time('getUser')
  const users = new Users()
  const user = await users.getUser(1)
  console.timeEnd('getUser')
  console.log(user)
}

start().catch(console.error)
```

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}}

* * *

&copy; 2016-20 Lloyd Brookes \<75pound@gmail.com\>.

Tested by [test-runner](https://github.com/test-runner-js/test-runner). Documented by [jsdoc-to-markdown](https://github.com/jsdoc2md/jsdoc-to-markdown).
