Skip to content

Commit

Permalink
Merge branch 'release/v0.4.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
mrister committed Jan 2, 2017
2 parents 0431f7c + cc0f265 commit b669058
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 53 deletions.
104 changes: 57 additions & 47 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ function redisStore(args) {
/* istanbul ignore next */
var redisOptions = getFromUrl(args) || args || {};
var poolSettings = redisOptions;
var Promise = args.promiseDependency || global.Promise;

redisOptions.host = redisOptions.host || '127.0.0.1';
redisOptions.port = redisOptions.port || 6379;
Expand Down Expand Up @@ -174,27 +175,32 @@ function redisStore(args) {
* @param {Object} [options] - The options (optional)
* @param {boolean|Object} options.compress - compression configuration
* @param {Function} cb - A callback that returns a potential error and the response
* @returns {Promise}
*/
self.get = function(key, options, cb) {
if (typeof options === 'function') {
cb = options;
options = {};
}
options = options || {};
options.parse = true;
return new Promise(function(resolve, reject) {
if (typeof options === 'function') {
cb = options;
options = {};
}
options = options || {};
options.parse = true;

var compress = (options.compress || options.compress === false) ? options.compress : redisOptions.compress;
if (compress) {
options.compress = (compress === true) ? compressDefault : compress;
key = new Buffer(key);
}
cb = cb ? cb : (err, result) => err ? reject(err) : resolve(result)

connect(function(err, conn) {
if (err) {
return cb && cb(err);
var compress = (options.compress || options.compress === false) ? options.compress : redisOptions.compress;
if (compress) {
options.compress = (compress === true) ? compressDefault : compress;
key = new Buffer(key);
}

conn.get(key, handleResponse(conn, cb, options));
connect(function(err, conn) {
if (err) {
return cb(err);
}

conn.get(key, handleResponse(conn, cb, options));
});
});
};

Expand All @@ -207,50 +213,54 @@ function redisStore(args) {
* @param {Object} options.ttl - The ttl value
* @param {boolean|Object} options.compress - compression configuration
* @param {Function} [cb] - A callback that returns a potential error, otherwise null
* @returns {Promise}
*/
self.set = function(key, value, options, cb) {
return new Promise(function(resolve, reject) {
if (typeof options === 'function') {
cb = options;
options = {};
}

if (typeof options === 'function') {
cb = options;
options = {};
}

if (!self.isCacheableValue(value)) {
return cb(new Error('value cannot be ' + value));
}
cb = cb ? cb : (err, result) => err ? reject(err) : resolve(result)

options = options || {};
if (!self.isCacheableValue(value)) {
return cb(new Error('value cannot be ' + value));
}

var ttl = (options.ttl || options.ttl === 0) ? options.ttl : redisOptions.ttl;
var compress = (options.compress || options.compress === false) ? options.compress : redisOptions.compress;
if (compress === true) {
compress = compressDefault;
}
options = options || {};

connect(function(err, conn) {
if (err) {
return cb && cb(err);
var ttl = (options.ttl || options.ttl === 0) ? options.ttl : redisOptions.ttl;
var compress = (options.compress || options.compress === false) ? options.compress : redisOptions.compress;
if (compress === true) {
compress = compressDefault;
}
var val = JSON.stringify(value) || '"undefined"';

// Refactored to remove duplicate code.
function persist(pErr, pVal) {
if (pErr) {
return cb && cb(pErr);
connect(function(err, conn) {
if (err) {
return cb(err);
}
var val = JSON.stringify(value) || '"undefined"';

if (ttl) {
conn.setex(key, ttl, pVal, handleResponse(conn, cb));
} else {
conn.set(key, pVal, handleResponse(conn, cb));
// Refactored to remove duplicate code.
function persist(pErr, pVal) {
if (pErr) {
return cb(pErr);
}

if (ttl) {
conn.setex(key, ttl, pVal, handleResponse(conn, cb));
} else {
conn.set(key, pVal, handleResponse(conn, cb));
}
}
}

if (compress) {
zlib.gzip(val, compress.params || {}, persist);
} else {
persist(null, val);
}
if (compress) {
zlib.gzip(val, compress.params || {}, persist);
} else {
persist(null, val);
}
});
});
};

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": "cache-manager-redis",
"version": "0.3.2",
"version": "0.4.0",
"description": "Redis store for the node-cache-manager",
"main": "index.js",
"scripts": {
Expand Down
55 changes: 50 additions & 5 deletions test/lib/redis-store-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,24 @@ describe ('initialization', function () {
});

describe('set', function () {
it('should return a promise', function (done) {
assert.ok(redisCache.set('foo', 'bar') instanceof Promise);
done();
});

it('should resolve promise on success', function (done) {
redisCache.set('foo', 'bar').then(result => {
assert.equal(result, 'OK');
done();
});
});

it('should reject promise on error', function (done) {
redisCache.set('foo', null)
.then(() => done(new Error ('Should reject')))
.catch(() => done());
});

it('should store a value without ttl', function (done) {
redisCache.set('foo', 'bar', function (err) {
assert.equal(err, null);
Expand All @@ -86,12 +104,12 @@ describe('set', function () {
});

it('should not be able to store a null value (not cacheable)', function (done) {
try {
redisCache.set('foo2', null);
redisCache.set('foo2', null, function (err) {
if (err) {
return done();
}
done(new Error('Null is not a valid value!'));
} catch (e) {
done();
}
});
});

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

describe('get', function () {
it('should return a promise', function (done) {
assert.ok(redisCache.get('foo') instanceof Promise);
done();
});

it('should resolve promise on success', function (done) {
redisCache.set('foo', 'bar')
.then(() => redisCache.get('foo'))
.then(result => {
assert.equal(result, 'bar');
done();
});
});

it('should reject promise on error', function (done) {
var pool = redisCache.store._pool;
sinon.stub(pool, 'acquireDb').yieldsAsync('Something unexpected');
sinon.stub(pool, 'release');
redisCache.get('foo')
.then(() => done(new Error ('Should reject')))
.catch(() => done())
.then(() => {
pool.acquireDb.restore();
pool.release.restore();
})
});

it('should retrieve a value for a given key', function (done) {
var value = 'bar';
redisCache.set('foo', value, function () {
Expand Down

0 comments on commit b669058

Please sign in to comment.