Browse Source

Add promise support for get/set

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

48
lib/multi_caching.js

@ -64,7 +64,11 @@ var multiCaching = function(caches, options) {
if (_isCacheableValue(result)) {
// break out of async loop.
if (typeof cb === 'function') {
return cb(err, result, i);
} else {
return cb.resolve(result);
}
}
i += 1;
@ -72,7 +76,17 @@ var multiCaching = function(caches, options) {
};
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) {
@ -85,7 +99,17 @@ var multiCaching = function(caches, options) {
} else {
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) {
getFromHighestPriorityCache(key, function(err, result, index) {
if (err) {
if (typeof cb === 'function') {
return cb(err);
} else {
return cb.reject(err);
}
}
if (typeof cb === 'function') {
cb(err, result);
} else {
cb.resolve(result);
}
if (index) {
var cachesToUpdate = caches.slice(0, index);
@ -227,7 +259,11 @@ var multiCaching = function(caches, 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 = {};
}
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);
if (cb) {
process.nextTick(cb);
} else {
return Promise.resolve(value);
}
};
@ -35,6 +37,7 @@ var memoryStore = function(args) {
cb = options;
}
var value = lruCache.get(key);
if (cb) {
process.nextTick(function() {
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() {

Loading…
Cancel
Save