cache-point
Advanced tools
Comparing version 1.0.0 to 2.0.0
32
index.js
@@ -6,32 +6,3 @@ const path = require('path') | ||
/** | ||
* A memoisation solution intended to cache the output of expensive operations, speeding up future invocations with the same input. | ||
* @module cache-point | ||
* @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) | ||
* }) | ||
* } | ||
*/ | ||
@@ -71,5 +42,6 @@ | ||
/** | ||
* A cache hit resolves with the stored value, a miss rejects. | ||
* A cache hit resolves with the stored value, a miss rejects with an `ENOENT` error code. | ||
* @param {*} - One or more values to uniquely identify the data. Can be any value, or an array of values of any type. | ||
* @returns {Promise} | ||
* @throws ENOENT | ||
*/ | ||
@@ -76,0 +48,0 @@ read (keys) { |
{ | ||
"name": "cache-point", | ||
"author": "Lloyd Brookes <75pound@gmail.com>", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"description": "Simple, filesystem-backed memoisation cache.", | ||
@@ -23,7 +23,7 @@ "repository": "https://github.com/75lb/cache-point", | ||
"devDependencies": { | ||
"jsdoc-to-markdown": "^5.0.2", | ||
"test-runner": "~0.6.0" | ||
"jsdoc-to-markdown": "^5.0.3", | ||
"test-runner": "~0.8.7" | ||
}, | ||
"dependencies": { | ||
"array-back": "^4.0.0", | ||
"array-back": "^4.0.1", | ||
"fs-then-native": "^2.0.0", | ||
@@ -30,0 +30,0 @@ "mkdirp2": "^1.0.4" |
[![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://david-dm.org/75lb/cache-point.svg)](https://david-dm.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) | ||
<a name="module_cache-point"></a> | ||
# cache-point | ||
## cache-point | ||
A memoisation solution intended to cache the output of expensive operations, speeding up future invocations with the same input. | ||
Simple, filesystem-backed memoisation cache. Use to cache the output of expensive operations speeding up future invocations with the same input. | ||
**Example** | ||
## Synopsis | ||
```js | ||
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) | ||
/* a mock function to simulate a slow remote request */ | ||
async function fetchUser (id) { | ||
return new Promise(resolve => { | ||
setTimeout(() => { | ||
resolve({ id, name: 'Layla' }) | ||
}, 1000) | ||
}) | ||
} | ||
// 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)) | ||
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 | ||
} | ||
} | ||
// 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) | ||
}) | ||
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 | ||
<a name="module_cache-point"></a> | ||
## cache-point | ||
* [cache-point](#module_cache-point) | ||
@@ -77,6 +113,10 @@ * [Cache](#exp_module_cache-point--Cache) ⏏ | ||
#### cache.read(keys) ⇒ <code>Promise</code> | ||
A cache hit resolves with the stored value, a miss rejects. | ||
A cache hit resolves with the stored value, a miss rejects with an `ENOENT` error code. | ||
**Kind**: instance method of [<code>Cache</code>](#exp_module_cache-point--Cache) | ||
**Throws**: | ||
- ENOENT | ||
| Param | Type | Description | | ||
@@ -147,2 +187,4 @@ | --- | --- | --- | | ||
© 2016-19 Lloyd Brookes \<75pound@gmail.com\>. Documented by [jsdoc-to-markdown](https://github.com/jsdoc2md/jsdoc-to-markdown). | ||
© 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). |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
13841
189
0
111
Updatedarray-back@^4.0.1