Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

cache-manager

Package Overview
Dependencies
Maintainers
1
Versions
110
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cache-manager - npm Package Compare versions

Comparing version 1.2.2 to 1.3.0

3

History.md

@@ -0,1 +1,4 @@

- 1.3.0 2016-01-26
- Promise support (#39, #24) - @PuKoren
- 1.2.2 2015-10-19

@@ -2,0 +5,0 @@ - Bugfix: Fixing domain error issues when error is thrown inside 'work' function (#28).

@@ -45,2 +45,20 @@ /** @module cacheManager/caching */

function wrapPromise(key, promise, options) {
return new Promise(function(resolve, reject) {
self.wrap(key, function(cb) {
Promise.resolve()
.then(promise)
.then(function(result) {
cb(null, result);
})
.catch(cb);
}, options, function(err, result) {
if (err) {
return reject(err);
}
resolve(result);
});
});
}
/**

@@ -73,2 +91,6 @@ * Wraps a function in cache. I.e., the first time the function is run,

if (!cb) {
return wrapPromise(key, work, options);
}
var hasKey = callbackFiller.has(key);

@@ -75,0 +97,0 @@ callbackFiller.add(key, {cb: cb, domain: process.domain});

@@ -50,2 +50,13 @@ /** @module cacheManager/multiCaching */

function getFromHighestPriorityCachePromise(key, options) {
return new Promise(function(resolve, reject) {
getFromHighestPriorityCache(key, options, function(err, result) {
if (err) {
return reject(err);
}
resolve(result);
});
});
}
function getFromHighestPriorityCache(key, options, cb) {

@@ -57,2 +68,6 @@ if (typeof options === 'function') {

if (!cb) {
return getFromHighestPriorityCachePromise(key, options);
}
var i = 0;

@@ -77,7 +92,25 @@ async.eachSeries(caches, function(cache, next) {

cache.store.get(key, options, callback);
}, cb);
}, function(err, result) {
return cb(err, result);
});
}
function setInMultipleCachesPromise(caches, opts) {
return new Promise(function(resolve, reject) {
setInMultipleCaches(caches, opts, function(err, result) {
if (err) {
return reject(err);
}
resolve(result);
});
});
}
function setInMultipleCaches(caches, opts, cb) {
opts.options = opts.options || {};
if (!cb) {
return setInMultipleCachesPromise(caches, opts);
}
async.each(caches, function(cache, next) {

@@ -91,5 +124,18 @@ var _isCacheableValue = getIsCacheableValueFunction(cache);

}
}, cb);
}, function(err, result) {
cb(err, result);
});
}
function getAndPassUpPromise(key) {
return new Promise(function(resolve, reject) {
self.getAndPassUp(key, function(err, result) {
if (err) {
return reject(err);
}
resolve(result);
});
});
}
/**

@@ -103,2 +149,6 @@ * Looks for an item in cache tiers.

self.getAndPassUp = function(key, cb) {
if (!cb) {
return getAndPassUpPromise(key);
}
getFromHighestPriorityCache(key, function(err, result, index) {

@@ -109,4 +159,2 @@ if (err) {

cb(err, result);
if (index) {

@@ -122,5 +170,25 @@ var cachesToUpdate = caches.slice(0, index);

}
return cb(err, result);
});
};
function wrapPromise(key, promise, options) {
return new Promise(function(resolve, reject) {
self.wrap(key, function(cb) {
Promise.resolve()
.then(promise)
.then(function(result) {
cb(null, result);
})
.catch(cb);
}, options, function(err, result) {
if (err) {
return reject(err);
}
resolve(result);
});
});
}
/**

@@ -155,2 +223,6 @@ * Wraps a function in one or more caches.

if (!cb) {
return wrapPromise(key, work, options);
}
var hasKey = callbackFiller.has(key);

@@ -176,4 +248,2 @@ callbackFiller.add(key, {cb: cb, domain: process.domain});

callbackFiller.fill(key, err);
} else {
cb(err);
}

@@ -223,3 +293,3 @@ })

setInMultipleCaches(caches, opts, cb);
return setInMultipleCaches(caches, opts, cb);
};

@@ -243,3 +313,3 @@

getFromHighestPriorityCache(key, options, cb);
return getFromHighestPriorityCache(key, options, cb);
};

@@ -246,0 +316,0 @@

@@ -30,2 +30,4 @@ var Lru = require("lru-cache");

process.nextTick(cb);
} else {
return Promise.resolve(value);
}

@@ -39,2 +41,3 @@ };

var value = lruCache.get(key);
if (cb) {

@@ -41,0 +44,0 @@ process.nextTick(function() {

3

package.json
{
"name": "cache-manager",
"version": "1.2.2",
"version": "1.3.0",
"description": "Cache module for Node.js",

@@ -28,2 +28,3 @@ "main": "index.js",

"coveralls": "^2.3.0",
"es6-promise": "^3.0.2",
"istanbul": "^0.2.11",

@@ -30,0 +31,0 @@ "jscs": "^1.9.0",

@@ -36,2 +36,4 @@ [![build status](https://secure.travis-ci.org/BryanDonovan/node-cache-manager.png)](http://travis-ci.org/BryanDonovan/node-cache-manager)

* [node-cache-manager-fs](https://github.com/hotelde/node-cache-manager-fs)
## Overview

@@ -140,2 +142,13 @@

#### Example Using Promises
```javascript
memoryCache.wrap(key, function() {
return getUserPromise(userId);
});
then(function(user) {
console.log('User:', user);
});
```
Here's a very basic example of how you could use this in an Express app:

@@ -142,0 +155,0 @@

@@ -676,2 +676,58 @@ // TODO: These are really a mix of unit and integration tests.

});
describe("using native promises", function() {
beforeEach(function() {
cache = caching({
store: 'memory',
max: 50,
ttl: 5 * 60
});
});
it("should be able to chain with simple promise", function(done) {
cache.wrap('key', function() {
return 'OK';
})
.then(function(res) {
assert.equal(res, 'OK');
done();
});
});
it("should be able to chain with cache function as a promise", function(done) {
cache.wrap('key', function() {
return new Promise(function(resolve) {
resolve('OK');
});
})
.then(function(res) {
assert.equal(res, 'OK');
done();
});
});
it("should be able to catch errors in cache function as a promise", function(done) {
cache.wrap('key', function() {
return new Promise(function(resolve, reject) {
reject('NOK');
});
})
.then(function() {
done(new Error('It should not call then since there is an error in the cache function!'));
})
.catch(function() {
done();
});
});
it("should be able to chain with non-cacheable value", function(done) {
cache.wrap('key', function() {
return;
})
.then(function(res) {
assert.equal(res, undefined);
done();
});
});
});
});

@@ -678,0 +734,0 @@

@@ -184,2 +184,32 @@ var assert = require('assert');

});
describe('using promises', function() {
it('gets data from first cache that has it', function(done) {
memoryCache3.set(key, value)
.then(function() {
return multiCache.get(key);
})
.then(function(result) {
assert.equal(result, value);
})
.then(done);
});
it("passes any options to underlying caches", function(done) {
var opts = {foo: 'bar'};
multiCache.set(key, value)
.then(function() {
sinon.spy(memoryCache.store, 'get');
return multiCache.get(key, opts);
})
.then(function(result) {
assert.equal(result, value);
assert.ok(memoryCache.store.get.calledWith(key, opts));
memoryCache.store.get.restore();
})
.then(done);
});
});
});

@@ -263,2 +293,13 @@

});
it("gets data from first cache that has it using promises", function(done) {
memoryCache3.set(key, value)
.then(function() {
return multiCache.getAndPassUp(key);
})
.then(function(result) {
assert.equal(result, value);
done();
});
});
});

@@ -328,2 +369,18 @@

it("checks to see if higher levels have item using promises", function(done) {
memoryCache3.set(key, value)
.then(function() {
return multiCache.getAndPassUp(key);
})
.then(function(result) {
assert.equal(result, value);
})
.then(function() {
process.nextTick(function() {
assert.equal(memoryCache.get(key), value);
});
})
.then(done);
});
context("when a cache store calls back with an error", function() {

@@ -353,2 +410,11 @@ var fakeError;

});
it("bubbles up errors from caches and reject promise", function(done) {
multiCache.getAndPassUp(key)
.catch(function(err) {
assert.ok(memoryStoreStub.get.called);
assert.equal(err, fakeError);
done();
});
});
});

@@ -977,2 +1043,66 @@ });

});
describe("using native promises", function() {
beforeEach(function() {
multiCache = multiCaching([memoryCache, memoryCache3]);
});
it("should be able to chain with simple promise", function(done) {
multiCache.wrap('key', function() {
return 'OK';
})
.then(function(res) {
assert.equal(res, 'OK');
done();
});
});
it("should be able to chain with cache function as a promise", function(done) {
multiCache.wrap('key', function() {
return new Promise(function(resolve) {
resolve('OK');
});
})
.then(function(res) {
assert.equal(res, 'OK');
done();
});
});
it("should be able to catch errors in cache function as a promise", function(done) {
multiCache.wrap('key', function() {
return new Promise(function(resolve, reject) {
reject('NOK');
});
})
.then(function() {
done(new Error('It should not call then since there is an error in the cache function!'));
})
.catch(function() {
done();
});
});
it("should be able to catch a throw in cache function as a promise", function(done) {
multiCache.wrap('key', function() {
throw 'NOK';
})
.then(function() {
done(new Error('It should not call then since there is an error in the cache function!'));
})
.catch(function() {
done();
});
});
it("should be able to chain with non-cacheable value", function(done) {
multiCache.wrap('key', function() {
return;
})
.then(function(res) {
assert.equal(res, undefined);
done();
});
});
});
});

@@ -979,0 +1109,0 @@

@@ -9,2 +9,6 @@ #!/usr/bin/env node

if (typeof Promise === "undefined") {
global.Promise = require('es6-promise').Promise;
}
var argv = optimist

@@ -11,0 +15,0 @@ .usage("Usage: $0 -t [types] --reporter [reporter] --timeout [timeout]")['default'](

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc