memoize-fs
Advanced tools
Comparing version 0.2.0 to 1.0.0
42
index.js
@@ -32,23 +32,19 @@ 'use strict'; | ||
function serialize(val) { | ||
if (!val) { return String(val); } | ||
if (typeof val === 'object') { | ||
/* jshint unused: vars */ | ||
var circRefColl = []; | ||
return JSON.stringify(val, function (name, value) { | ||
if (typeof value === 'function') { | ||
return; // ignore arguments and attributes of type function silently | ||
function serialize() { | ||
/* jshint unused: vars */ | ||
var circRefColl = []; | ||
return JSON.stringify(args, function (name, value) { | ||
if (typeof value === 'function') { | ||
return; // ignore arguments and attributes of type function silently | ||
} | ||
if (typeof value === 'object' && value !== null) { | ||
if (circRefColl.indexOf(value) !== -1) { | ||
// circular reference found, discard key | ||
return; | ||
} | ||
if (typeof value === 'object' && value !== null) { | ||
if (circRefColl.indexOf(value) !== -1) { | ||
// circular reference found, discard key | ||
return; | ||
} | ||
// store value in collection | ||
circRefColl.push(value); | ||
} | ||
return value; | ||
}); | ||
} | ||
return String(val); | ||
// store value in collection | ||
circRefColl.push(value); | ||
} | ||
return value; | ||
}); | ||
} | ||
@@ -61,7 +57,3 @@ | ||
if (opt.serialize !== undefined) { | ||
argsStr = serialize(opt.serialize); | ||
} else { | ||
argsStr = serialize(args); | ||
} | ||
argsStr = serialize(args); | ||
@@ -68,0 +60,0 @@ hash = crypto.createHash('md5').update(fnStr + argsStr + salt).digest('hex'); |
{ | ||
"name": "memoize-fs", | ||
"version": "0.2.0", | ||
"version": "1.0.0", | ||
"description": "memoize/cache in file system solution for Node.js", | ||
@@ -33,10 +33,10 @@ "author": "Boris Diakur <contact@borisdiakur.com> (https://github.com/borisdiakur)", | ||
"devDependencies": { | ||
"coveralls": "~2.11.0", | ||
"coveralls": "^2.11.0", | ||
"grunt": "~0.4.5", | ||
"grunt-cli": "~0.1.13", | ||
"grunt-contrib-jshint": "~0.11.0", | ||
"grunt-contrib-jshint": "^0.11.0", | ||
"grunt-nsp-package": "~0.0.5", | ||
"istanbul": "~0.3.5", | ||
"mocha": "~2.1.0", | ||
"mocha-lcov-reporter": "0.0.1" | ||
"istanbul": "^0.3.5", | ||
"mocha": "^2.2.5", | ||
"mocha-lcov-reporter": "0.0.2" | ||
}, | ||
@@ -43,0 +43,0 @@ "private": false, |
@@ -5,4 +5,4 @@ # memoize-fs | ||
[![Build Status](https://api.travis-ci.org/borisdiakur/memoize-fs.png?branch=master)](https://travis-ci.org/borisdiakur/memoize-fs) | ||
[![Coverage Status](https://img.shields.io/coveralls/borisdiakur/memoize-fs.svg)](https://coveralls.io/r/borisdiakur/memoize-fs) | ||
[![Build Status](https://travis-ci.org/borisdiakur/memoize-fs.svg?branch=master)](https://travis-ci.org/borisdiakur/memoize-fs) | ||
[![Coverage Status](https://coveralls.io/repos/borisdiakur/memoize-fs/badge.svg?branch=master)](https://coveralls.io/r/borisdiakur/memoize-fs?branch=master) | ||
[![Dependency Status](https://gemnasium.com/borisdiakur/memoize-fs.svg)](https://gemnasium.com/borisdiakur/memoize-fs) | ||
@@ -20,3 +20,3 @@ | ||
* Works with almost all kind and any length of function arguments – [__custom serialization is posible__](#serialize) | ||
* Works with almost all kind and any length of function arguments ([__serialization__](#serialization) is handled under the hood) | ||
* Supports memoization of [__asynchronous functions__](#memoizing-asynchronous-functions) | ||
@@ -37,3 +37,4 @@ * Supports memoization of [__promisified functions__](#memoizing-promisified-functions) | ||
```javascript | ||
var memoize = require('memoize-fs')({ cachePath: require('path').join(__dirname, '../../cache' }), | ||
var cachePath = require('path').join(__dirname, '..', 'cache'), | ||
memoize = require('memoize-fs')({ cachePath: cachePath }), | ||
fun = function (a, b) { return a + b; }; | ||
@@ -114,3 +115,3 @@ | ||
```javascript | ||
memoize.fn(fun, { cacheId: 'foobar'}).then(... | ||
memoize.fn(fun, { cacheId: 'foobar' }).then(... | ||
``` | ||
@@ -123,3 +124,3 @@ | ||
```javascript | ||
memoize.fn(fun, { salt: 'foobar'}).then(... | ||
memoize.fn(fun, { salt: 'foobar' }).then(... | ||
``` | ||
@@ -132,26 +133,13 @@ | ||
```javascript | ||
memoize.fn(fun, { force: true}).then(... | ||
memoize.fn(fun, { force: true }).then(... | ||
``` | ||
#### serialize | ||
#### noBody | ||
memoize-fs tries to serialize the arguments of the memoized function in order to create a hash which is used as the name of the cache file to be stored or retrieved. | ||
The hash is created from the serialized arguments, the function body and the [salt](https://github.com/borisdiakur/memoize-fs#salt) (if provided as an option). | ||
If you want memoize-fs to use a custom key instead of letting it serialize the arguments, you can pass the key in the `serialize` option to memoize-fs: | ||
If for some reason you want to omit the function body when generating the hash ([see serialization](#serialization)), set the option `noBody` to `true`. | ||
```javascript | ||
memoize.fn(fun, { serialize: 'foobar'}).then(... | ||
memoize.fn(fun, { noBody: true }).then(... | ||
``` | ||
Alternatively you can pass another object to be serialized in place of the arguments of the memoized function: | ||
```javascript | ||
memoize.fn(fun, { serialize: { foo: 'bar'}}).then(... | ||
``` | ||
#### noBody | ||
The hash is created from the serialized arguments, the function body and the [salt](https://github.com/borisdiakur/memoize-fs#salt) (if provided as an option). | ||
If for some reason you want to omit the function body when generating the hash, set the option `noBody` to `true`. | ||
### Manual cache invalidation | ||
@@ -171,2 +159,8 @@ | ||
## Serialization | ||
memoize-fs tries to serialize the arguments of the memoized function in order to create a hash which is used as the name of the cache file to be stored or retrieved. | ||
The hash is created from the serialized arguments, the function body and the [salt](#salt) (if provided as an option). | ||
__Note that memoize-fs serializes arguments using JSON. While it checks for circular references, it ignores arguments and attributes of type function silently.__ | ||
## Common pitfalls | ||
@@ -180,3 +174,2 @@ | ||
__These arguments will be ignored silently during serialization__. | ||
To avoid flawy caching please use [__custom serialization__](#serialize). | ||
@@ -183,0 +176,0 @@ ## Contributing |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
0
24442
216
192