Browse Source

Add promise support for get/set

feature/remove-domains
Jonathan 9 years ago
parent
commit
74d4e4b2d4
  1. 54
      lib/multi_caching.js
  2. 3
      lib/stores/memory.js
  3. 13
      test/multi_caching.unit.js

54
lib/multi_caching.js

@ -64,7 +64,11 @@ var multiCaching = function(caches, options) {
if (_isCacheableValue(result)) { if (_isCacheableValue(result)) {
// break out of async loop. // break out of async loop.
return cb(err, result, i); if (typeof cb === 'function') {
return cb(err, result, i);
} else {
return cb.resolve(result);
}
} }
i += 1; i += 1;
@ -72,7 +76,17 @@ var multiCaching = function(caches, options) {
}; };
cache.store.get(key, options, callback); cache.store.get(key, options, callback);
}, cb); }, function(err, result) {
if (typeof cb === 'object') {
if (err) {
cb.reject(err);
} else {
cb.resolve(result);
}
} else if (typeof cb === 'function') {
cb(err, result);
}
});
} }
function setInMultipleCaches(caches, opts, cb) { function setInMultipleCaches(caches, opts, cb) {
@ -85,7 +99,17 @@ var multiCaching = function(caches, options) {
} else { } else {
next(); next();
} }
}, cb); }, function(err, result) {
if (typeof cb === 'object') {
if (err) {
cb.reject(err);
} else {
cb.resolve(result);
}
} else if (typeof cb === 'function') {
cb(err, result);
}
});
} }
/** /**
@ -98,10 +122,18 @@ var multiCaching = function(caches, options) {
self.getAndPassUp = function(key, cb) { self.getAndPassUp = function(key, cb) {
getFromHighestPriorityCache(key, function(err, result, index) { getFromHighestPriorityCache(key, function(err, result, index) {
if (err) { if (err) {
return cb(err); if (typeof cb === 'function') {
return cb(err);
} else {
return cb.reject(err);
}
} }
cb(err, result); if (typeof cb === 'function') {
cb(err, result);
} else {
cb.resolve(result);
}
if (index) { if (index) {
var cachesToUpdate = caches.slice(0, index); var cachesToUpdate = caches.slice(0, index);
@ -227,7 +259,11 @@ var multiCaching = function(caches, options) {
options: options options: options
}; };
setInMultipleCaches(caches, opts, cb); var defer = Promise.defer();
setInMultipleCaches(caches, opts, cb || defer);
return defer.promise;
}; };
/** /**
@ -246,7 +282,11 @@ var multiCaching = function(caches, options) {
options = {}; options = {};
} }
getFromHighestPriorityCache(key, options, cb); var defer = Promise.defer();
getFromHighestPriorityCache(key, options, cb || defer);
return defer.promise;
}; };
/** /**

3
lib/stores/memory.js

@ -27,6 +27,8 @@ var memoryStore = function(args) {
lruCache.set(key, value, maxAge); lruCache.set(key, value, maxAge);
if (cb) { if (cb) {
process.nextTick(cb); process.nextTick(cb);
} else {
return Promise.resolve(value);
} }
}; };
@ -35,6 +37,7 @@ var memoryStore = function(args) {
cb = options; cb = options;
} }
var value = lruCache.get(key); var value = lruCache.get(key);
if (cb) { if (cb) {
process.nextTick(function() { process.nextTick(function() {
cb(null, value); cb(null, value);

13
test/multi_caching.unit.js

@ -181,6 +181,19 @@ describe("multiCaching", function() {
}); });
}); });
}); });
describe('using promises', function() {
it('should return a promise and resolve it', function(done) {
memoryCache3.set(key, value)
.then(function() {
return multiCache.get(key);
})
.then(function(result) {
assert.equal(result, value);
})
.then(done);
});
});
}); });
describe("del()", function() { describe("del()", function() {

Loading…
Cancel
Save