JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 279530
  • Score
    100M100P100Q169702F
  • License MIT

Simple, filesystem-backed memoisation cache.

Package Exports

  • cache-point

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (cache-point) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

view on npm npm module downloads Build Status Dependency Status js-standard-style

cache-point

A memoisation solution intended to cache the output of expensive operations, speeding up future invocations with the same input.

Example

const Cache = require('cache-point')
const cache = new Cache({ dir: 'tmp/example' })

// The first invocation will take 3s, the rest instantaneous.
// outputs: 'result'
getData('some input')
  .then(console.log)

// check the cache for output generated with this input.
// cache.read() will resolve on hit, reject on miss.
function getData (input) {
  return cache
    .read(input)
    .catch(() => expensiveOperation(input))
}

// The expensive operation we're aiming to avoid,
// (3 second cost per invocation)
function expensiveOperation (input) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      const output = 'result'
      cache.write(input, output)
      resolve(output)
    }, 3000)
  })
}

Cache ⏏

Kind: Exported class

new Cache([options])

Param Type
[options] object
[options.dir] string

cache.dir : string

Current cache directory. Can be changed at any time.

Kind: instance property of Cache

cache.read(keys) ⇒ Promise

A cache hit resolves with the stored value, a miss rejects.

Kind: instance method of Cache

Param Type Description
keys * One or more values to uniquely identify the data. Can be any value, or an array of values of any type.

cache.readSync(keys) ⇒ string

A cache hit returns the stored value, a miss returns null.

Kind: instance method of Cache

Param Type Description
keys * One or more values to uniquely identify the data. Can be any value, or an array of values of any type.

cache.write(keys, content) ⇒ Promise

Write some data to the cache. Returns a promise which resolves when the write is complete.

Kind: instance method of Cache

Param Type Description
keys * One or more values to index the data, e.g. a request object or set of function args.
content * the data to store

cache.writeSync(keys, content)

Write some data to the cache with a key.

Kind: instance method of Cache

Param Type Description
keys * One or more values to index the data, e.g. a request object or set of function args.
content * the data to store

cache.getChecksum(keys) ⇒ string

Used internally to convert a key value into a hex checksum. Override if for some reason you need a different hashing strategy.

Kind: instance method of Cache

Param Type Description
keys * One or more values to index the data, e.g. a request object or set of function args.

cache.clear() ⇒ Promise

Clears the cache. Returns a promise which resolves once the cache is clear.

Kind: instance method of Cache

cache.remove() ⇒ Promise

Clears and removes the cache directory. Returns a promise which resolves once the remove is complete.

Kind: instance method of Cache


© 2016-19 Lloyd Brookes <75pound@gmail.com>. Documented by jsdoc-to-markdown.