Skip to content

Commit b669058

Browse files
committed
Merge branch 'release/v0.4.0'
2 parents 0431f7c + cc0f265 commit b669058

File tree

3 files changed

+108
-53
lines changed

3 files changed

+108
-53
lines changed

index.js

Lines changed: 57 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ function redisStore(args) {
2929
/* istanbul ignore next */
3030
var redisOptions = getFromUrl(args) || args || {};
3131
var poolSettings = redisOptions;
32+
var Promise = args.promiseDependency || global.Promise;
3233

3334
redisOptions.host = redisOptions.host || '127.0.0.1';
3435
redisOptions.port = redisOptions.port || 6379;
@@ -174,27 +175,32 @@ function redisStore(args) {
174175
* @param {Object} [options] - The options (optional)
175176
* @param {boolean|Object} options.compress - compression configuration
176177
* @param {Function} cb - A callback that returns a potential error and the response
178+
* @returns {Promise}
177179
*/
178180
self.get = function(key, options, cb) {
179-
if (typeof options === 'function') {
180-
cb = options;
181-
options = {};
182-
}
183-
options = options || {};
184-
options.parse = true;
181+
return new Promise(function(resolve, reject) {
182+
if (typeof options === 'function') {
183+
cb = options;
184+
options = {};
185+
}
186+
options = options || {};
187+
options.parse = true;
185188

186-
var compress = (options.compress || options.compress === false) ? options.compress : redisOptions.compress;
187-
if (compress) {
188-
options.compress = (compress === true) ? compressDefault : compress;
189-
key = new Buffer(key);
190-
}
189+
cb = cb ? cb : (err, result) => err ? reject(err) : resolve(result)
191190

192-
connect(function(err, conn) {
193-
if (err) {
194-
return cb && cb(err);
191+
var compress = (options.compress || options.compress === false) ? options.compress : redisOptions.compress;
192+
if (compress) {
193+
options.compress = (compress === true) ? compressDefault : compress;
194+
key = new Buffer(key);
195195
}
196196

197-
conn.get(key, handleResponse(conn, cb, options));
197+
connect(function(err, conn) {
198+
if (err) {
199+
return cb(err);
200+
}
201+
202+
conn.get(key, handleResponse(conn, cb, options));
203+
});
198204
});
199205
};
200206

@@ -207,50 +213,54 @@ function redisStore(args) {
207213
* @param {Object} options.ttl - The ttl value
208214
* @param {boolean|Object} options.compress - compression configuration
209215
* @param {Function} [cb] - A callback that returns a potential error, otherwise null
216+
* @returns {Promise}
210217
*/
211218
self.set = function(key, value, options, cb) {
219+
return new Promise(function(resolve, reject) {
220+
if (typeof options === 'function') {
221+
cb = options;
222+
options = {};
223+
}
212224

213-
if (typeof options === 'function') {
214-
cb = options;
215-
options = {};
216-
}
217-
218-
if (!self.isCacheableValue(value)) {
219-
return cb(new Error('value cannot be ' + value));
220-
}
225+
cb = cb ? cb : (err, result) => err ? reject(err) : resolve(result)
221226

222-
options = options || {};
227+
if (!self.isCacheableValue(value)) {
228+
return cb(new Error('value cannot be ' + value));
229+
}
223230

224-
var ttl = (options.ttl || options.ttl === 0) ? options.ttl : redisOptions.ttl;
225-
var compress = (options.compress || options.compress === false) ? options.compress : redisOptions.compress;
226-
if (compress === true) {
227-
compress = compressDefault;
228-
}
231+
options = options || {};
229232

230-
connect(function(err, conn) {
231-
if (err) {
232-
return cb && cb(err);
233+
var ttl = (options.ttl || options.ttl === 0) ? options.ttl : redisOptions.ttl;
234+
var compress = (options.compress || options.compress === false) ? options.compress : redisOptions.compress;
235+
if (compress === true) {
236+
compress = compressDefault;
233237
}
234-
var val = JSON.stringify(value) || '"undefined"';
235238

236-
// Refactored to remove duplicate code.
237-
function persist(pErr, pVal) {
238-
if (pErr) {
239-
return cb && cb(pErr);
239+
connect(function(err, conn) {
240+
if (err) {
241+
return cb(err);
240242
}
243+
var val = JSON.stringify(value) || '"undefined"';
241244

242-
if (ttl) {
243-
conn.setex(key, ttl, pVal, handleResponse(conn, cb));
244-
} else {
245-
conn.set(key, pVal, handleResponse(conn, cb));
245+
// Refactored to remove duplicate code.
246+
function persist(pErr, pVal) {
247+
if (pErr) {
248+
return cb(pErr);
249+
}
250+
251+
if (ttl) {
252+
conn.setex(key, ttl, pVal, handleResponse(conn, cb));
253+
} else {
254+
conn.set(key, pVal, handleResponse(conn, cb));
255+
}
246256
}
247-
}
248257

249-
if (compress) {
250-
zlib.gzip(val, compress.params || {}, persist);
251-
} else {
252-
persist(null, val);
253-
}
258+
if (compress) {
259+
zlib.gzip(val, compress.params || {}, persist);
260+
} else {
261+
persist(null, val);
262+
}
263+
});
254264
});
255265
};
256266

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cache-manager-redis",
3-
"version": "0.3.2",
3+
"version": "0.4.0",
44
"description": "Redis store for the node-cache-manager",
55
"main": "index.js",
66
"scripts": {

test/lib/redis-store-spec.js

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,24 @@ describe ('initialization', function () {
6060
});
6161

6262
describe('set', function () {
63+
it('should return a promise', function (done) {
64+
assert.ok(redisCache.set('foo', 'bar') instanceof Promise);
65+
done();
66+
});
67+
68+
it('should resolve promise on success', function (done) {
69+
redisCache.set('foo', 'bar').then(result => {
70+
assert.equal(result, 'OK');
71+
done();
72+
});
73+
});
74+
75+
it('should reject promise on error', function (done) {
76+
redisCache.set('foo', null)
77+
.then(() => done(new Error ('Should reject')))
78+
.catch(() => done());
79+
});
80+
6381
it('should store a value without ttl', function (done) {
6482
redisCache.set('foo', 'bar', function (err) {
6583
assert.equal(err, null);
@@ -86,12 +104,12 @@ describe('set', function () {
86104
});
87105

88106
it('should not be able to store a null value (not cacheable)', function (done) {
89-
try {
90-
redisCache.set('foo2', null);
107+
redisCache.set('foo2', null, function (err) {
108+
if (err) {
109+
return done();
110+
}
91111
done(new Error('Null is not a valid value!'));
92-
} catch (e) {
93-
done();
94-
}
112+
});
95113
});
96114

97115
it('should store a value without callback', function (done) {
@@ -152,6 +170,33 @@ describe('set', function () {
152170
});
153171

154172
describe('get', function () {
173+
it('should return a promise', function (done) {
174+
assert.ok(redisCache.get('foo') instanceof Promise);
175+
done();
176+
});
177+
178+
it('should resolve promise on success', function (done) {
179+
redisCache.set('foo', 'bar')
180+
.then(() => redisCache.get('foo'))
181+
.then(result => {
182+
assert.equal(result, 'bar');
183+
done();
184+
});
185+
});
186+
187+
it('should reject promise on error', function (done) {
188+
var pool = redisCache.store._pool;
189+
sinon.stub(pool, 'acquireDb').yieldsAsync('Something unexpected');
190+
sinon.stub(pool, 'release');
191+
redisCache.get('foo')
192+
.then(() => done(new Error ('Should reject')))
193+
.catch(() => done())
194+
.then(() => {
195+
pool.acquireDb.restore();
196+
pool.release.restore();
197+
})
198+
});
199+
155200
it('should retrieve a value for a given key', function (done) {
156201
var value = 'bar';
157202
redisCache.set('foo', value, function () {

0 commit comments

Comments
 (0)