Skip to content

Commit

Permalink
Enhanced documentation. Added option noBody.
Browse files Browse the repository at this point in the history
References:

    closes #2
  • Loading branch information
Boris Diakur committed Apr 28, 2015
1 parent bd6070f commit 857ad45
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 37 deletions.
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,23 +105,23 @@ By default all cache files are saved into the __root cache__ which is the folder
var memoize = require('memoize-fs')({ cachePath: require('path').join(__dirname, '../../cache' });
```
The cacheId option which you can specify during momoization of a function resolves to the name of a subfolder created inside the root cache folder. Cached function calls will be cached inside that folder:
The `cacheId` option which you can specify during momoization of a function resolves to the name of a subfolder created inside the root cache folder. Cached function calls will be cached inside that folder:
```javascript
memoize.fn(fun, { cacheId: 'foobar'}).then(...
```
#### salt
Functions may have references to variables outside their own scope. As a consequence two functions which look exactly the same (they have the same function signature and function body) can return different results even when executed with identical arguments. In order to avoid the same cache being used for two different functions you can use the salt option which mutates the hash key created for the memoized function which in turn defines the name of the cache file:
Functions may have references to variables outside their own scope. As a consequence two functions which look exactly the same (they have the same function signature and function body) can return different results even when executed with identical arguments. In order to avoid the same cache being used for two different functions you can use the `salt` option which mutates the hash key created for the memoized function which in turn defines the name of the cache file:
```javascript
memoize.fn(fun, { salt: 'foobar'}).then(...
```
#### force
The force option forces the re-execution of an already memoized function and the re-caching of its outcome:
The `force` option forces the re-execution of an already memoized function and the re-caching of its outcome:
```javascript
memoize.fn(fun, { force: true}).then(...
Expand All @@ -130,7 +130,8 @@ memoize.fn(fun, { force: true}).then(...
#### serialize
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.
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:
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:
```javascript
memoize.fn(fun, { serialize: 'foobar'}).then(...
Expand All @@ -142,6 +143,11 @@ Alternatively you can pass another object to be serialized in place of the argum
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
You can delete the root cache (all cache files inside the folder specified by the cachePath option):
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ module.exports = function (options) {
}

var salt = opt.salt || '',
fnStr = String(fn),
fnStr = (opt.noBody ? '' : String(fn)),
argsStr,
hash;

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "memoize-fs",
"version": "0.1.10",
"version": "0.2.0",
"description": "memoize/cache in file system solution for Node.js",
"author": "Boris Diakur <contact@borisdiakur.com> (https://github.com/borisdiakur)",
"scripts": {
Expand Down
109 changes: 78 additions & 31 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -700,19 +700,17 @@ describe('memoize-fs', function () {
memoize.fn(function (a, b) { return a + b + c; }, { cacheId: 'foobar', serialize: 'qux' }).then(function (memFn) {
memFn(1, 2).then(function (result) {
assert.strictEqual(result, 6, 'expected result to strictly equal 6');
memoize.fn(function (a, b) { return a + b + c; }, { cacheId: 'foobar', serialize: 'qux' }).then(function (memFn) {
c = 999;
memFn(1, 2).then(function (result) {
assert.strictEqual(result, 6, 'expected result to strictly equal 6');
fs.readdir(path.join(cachePath, 'foobar'), function (err, files) {
if (err) {
done(err);
} else {
assert.strictEqual(files.length, 1, 'expected exactly one file in cache with id foobar');
done();
}
});
}, done);
c = 999;
memFn(1, 2).then(function (result) {
assert.strictEqual(result, 6, 'expected result to strictly equal 6');
fs.readdir(path.join(cachePath, 'foobar'), function (err, files) {
if (err) {
done(err);
} else {
assert.strictEqual(files.length, 1, 'expected exactly one file in cache with id foobar');
done();
}
});
}, done);
}, done);
}, done);
Expand All @@ -725,15 +723,66 @@ describe('memoize-fs', function () {
memoize.fn(function (a, b) { return a + b + c; }, { cacheId: 'foobar', serialize: null }).then(function (memFn) {
memFn(1, 2).then(function (result) {
assert.strictEqual(result, 6, 'expected result to strictly equal 6');
memoize.fn(function (a, b) { return a + b + c; }, { cacheId: 'foobar', serialize: null }).then(function (memFn) {
c = 999;
c = 999;
memFn(1, 2).then(function (result) {
assert.strictEqual(result, 6, 'expected result to strictly equal 6');
fs.readdir(path.join(cachePath, 'foobar'), function (err, files) {
if (err) {
done(err);
} else {
assert.strictEqual(files.length, 1, 'expected exactly one file in cache with id foobar');
done();
}
});
}, done);
}, done);
}, done);
});

it('should cache the result of a memoized function on second execution with option serialize of type object', function (done) {
var cachePath = path.join(__dirname, '../build/cache'),
memoize = memoizeFs({ cachePath: cachePath }),
c = 3,
Circ = function () {
this.abc = 'Hello';
this.circular = this;
},
serializeObj = {qux: 321, circular: new Circ(), fun: function () {}};
memoize.fn(function (a, b) { return a + b + c; }, { cacheId: 'foobar', serialize: serializeObj }).then(function (memFn) {
memFn(1, 2).then(function (result) {
assert.strictEqual(result, 6, 'expected result to strictly equal 6');
c = 999;
memFn(1, 2).then(function (result) {
assert.strictEqual(result, 6, 'expected result to strictly equal 6');
fs.readdir(path.join(cachePath, 'foobar'), function (err, files) {
if (err) {
done(err);
} else {
assert.strictEqual(files.length, 1, 'expected exactly one file in cache with id foobar');
done();
}
});
}, done);
}, done);
}, done);
});

it('should cache the results of two equal memoized functions with different options serialize set', function (done) {
var cachePath = path.join(__dirname, '../build/cache'),
memoize = memoizeFs({ cachePath: cachePath }),
c = 3;
memoize.fn(function (a, b) { return a + b + c; }, { cacheId: 'foobar', serialize: {qux: 321} }).then(function (memFn) {
memFn(1, 2).then(function (result) {
assert.strictEqual(result, 6, 'expected result to strictly equal 6');
memoize.fn(function (a, b) { return a + b + c; }, { cacheId: 'foobar', serialize: {qux: 123} }).then(function (memFn) {
c = 4;
memFn(1, 2).then(function (result) {
assert.strictEqual(result, 6, 'expected result to strictly equal 6');
assert.strictEqual(result, 7, 'expected result to strictly equal 6');
fs.readdir(path.join(cachePath, 'foobar'), function (err, files) {
if (err) {
done(err);
} else {
assert.strictEqual(files.length, 1, 'expected exactly one file in cache with id foobar');
assert.strictEqual(files.length, 2, 'expected exactly two files in cache with id foobar');
done();
}
});
Expand All @@ -742,20 +791,18 @@ describe('memoize-fs', function () {
}, done);
}, done);
});
});

it('should cache the result of a memoized function on second execution with option serialize of type object', function (done) {
describe('noBody', function () {

it('should cache the result of a memoized function on second execution with option noBody set to true with different function names', function (done) {
var cachePath = path.join(__dirname, '../build/cache'),
memoize = memoizeFs({ cachePath: cachePath }),
c = 3,
Circ = function () {
this.abc = 'Hello';
this.circular = this;
},
serializeObj = {qux: 321, circular: new Circ(), fun: function () {}};
memoize.fn(function (a, b) { return a + b + c; }, { cacheId: 'foobar', serialize: serializeObj }).then(function (memFn) {
c = 3;
memoize.fn(function foo(a, b) { return a + b + c; }, { cacheId: 'foobar', serialize: 'qux', noBody: true }).then(function (memFn) {
memFn(1, 2).then(function (result) {
assert.strictEqual(result, 6, 'expected result to strictly equal 6');
memoize.fn(function (a, b) { return a + b + c; }, { cacheId: 'foobar', serialize: serializeObj }).then(function (memFn) {
memoize.fn(function bar(a, b) { return a + b + c; }, { cacheId: 'foobar', serialize: 'qux', noBody: true }).then(function (memFn) {
c = 999;
memFn(1, 2).then(function (result) {
assert.strictEqual(result, 6, 'expected result to strictly equal 6');
Expand All @@ -773,17 +820,17 @@ describe('memoize-fs', function () {
}, done);
});

it('should cache the results of two equal memoized functions with different options serialize set', function (done) {
it('should not cache the result of a memoized function on second execution with option noBody not set with different function names', function (done) {
var cachePath = path.join(__dirname, '../build/cache'),
memoize = memoizeFs({ cachePath: cachePath }),
c = 3;
memoize.fn(function (a, b) { return a + b + c; }, { cacheId: 'foobar', serialize: {qux: 321} }).then(function (memFn) {
memoize.fn(function foo(a, b) { return a + b + c; }, { cacheId: 'foobar', serialize: 'qux' }).then(function (memFn) {
memFn(1, 2).then(function (result) {
assert.strictEqual(result, 6, 'expected result to strictly equal 6');
memoize.fn(function (a, b) { return a + b + c; }, { cacheId: 'foobar', serialize: {qux: 123} }).then(function (memFn) {
c = 4;
memoize.fn(function bar(a, b) { return a + b + c; }, { cacheId: 'foobar', serialize: 'qux' }).then(function (memFn) {
c = 999;
memFn(1, 2).then(function (result) {
assert.strictEqual(result, 7, 'expected result to strictly equal 6');
assert.strictEqual(result, 1002, 'expected result to strictly equal 1002');
fs.readdir(path.join(cachePath, 'foobar'), function (err, files) {
if (err) {
done(err);
Expand Down

0 comments on commit 857ad45

Please sign in to comment.