Skip to content

Commit

Permalink
Add promise support to del, reset, and ttl
Browse files Browse the repository at this point in the history
  • Loading branch information
dabroek committed Dec 15, 2017
1 parent dd7f916 commit 809fec2
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 15 deletions.
36 changes: 30 additions & 6 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,30 @@ var redisStore = function redisStore() {
});
},
del: function del(key, options, cb) {
if (typeof options === 'function') {
cb = options;
}
return new Promise(function (resolve, reject) {
if (typeof options === 'function') {
cb = options;
}

if (!cb) {
cb = function cb(err, result) {
return err ? reject(err) : resolve(result);
};
}

redisCache.del(key, handleResponse(cb));
redisCache.del(key, handleResponse(cb));
});
},
reset: function reset(cb) {
return redisCache.flushdb(handleResponse(cb));
return new Promise(function (resolve, reject) {
if (!cb) {
cb = function cb(err, result) {
return err ? reject(err) : resolve(result);
};
}

redisCache.flushdb(handleResponse(cb));
});
},
keys: function keys(pattern, cb) {
return new Promise(function (resolve, reject) {
Expand All @@ -79,7 +95,15 @@ var redisStore = function redisStore() {
});
},
ttl: function ttl(key, cb) {
return redisCache.ttl(key, handleResponse(cb));
return new Promise(function (resolve, reject) {
if (!cb) {
cb = function cb(err, result) {
return err ? reject(err) : resolve(result);
};
}

redisCache.ttl(key, handleResponse(cb));
});
},
isCacheableValue: storeArgs.is_cacheable_value || function (value) {
return value !== undefined && value !== null;
Expand Down
38 changes: 30 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,28 @@ const redisStore = (...args) => {
redisCache.get(key, handleResponse(cb, { parse: true }));
})
),
del: (key, options, cb) => {
if (typeof options === 'function') {
cb = options;
}
del: (key, options, cb) => (
new Promise((resolve, reject) => {
if (typeof options === 'function') {
cb = options;
}

redisCache.del(key, handleResponse(cb));
},
reset: cb => redisCache.flushdb(handleResponse(cb)),
if (!cb) {
cb = (err, result) => (err ? reject(err) : resolve(result));
}

redisCache.del(key, handleResponse(cb));
})
),
reset: cb => (
new Promise((resolve, reject) => {
if (!cb) {
cb = (err, result) => (err ? reject(err) : resolve(result));
}

redisCache.flushdb(handleResponse(cb));
})
),
keys: (pattern, cb) => (
new Promise((resolve, reject) => {
if (typeof pattern === 'function') {
Expand All @@ -64,7 +78,15 @@ const redisStore = (...args) => {
redisCache.keys(pattern, handleResponse(cb));
})
),
ttl: (key, cb) => redisCache.ttl(key, handleResponse(cb)),
ttl: (key, cb) => (
new Promise((resolve, reject) => {
if (!cb) {
cb = (err, result) => (err ? reject(err) : resolve(result));
}

redisCache.ttl(key, handleResponse(cb));
})
),
isCacheableValue: storeArgs.is_cacheable_value || (value => value !== undefined && value !== null),
};
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "cache-manager-redis-store",
"author": "Matthijs Dabroek <[email protected]>",
"description": "Redis store for node-cache-manager",
"version": "1.2.0",
"version": "1.3.0",
"license": "MIT",
"main": "dist/index.js",
"repository": {
Expand Down
15 changes: 15 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,11 @@ describe('get', () => {
});

describe('del', () => {
it('should return a promise', (done) => {
expect(redisCache.del('foo')).toBeInstanceOf(Promise);
done();
});

it('should delete a value for a given key', (done) => {
redisCache.set('foo', 'bar', () => {
redisCache.del('foo', (err) => {
Expand All @@ -269,6 +274,11 @@ describe('del', () => {
});

describe('reset', () => {
it('should return a promise', (done) => {
expect(redisCache.reset()).toBeInstanceOf(Promise);
done();
});

it('should flush underlying db', (done) => {
redisCache.reset((err) => {
expect(err).toEqual(null);
Expand All @@ -291,6 +301,11 @@ describe('reset', () => {
});

describe('ttl', () => {
it('should return a promise', (done) => {
expect(redisCache.ttl('foo')).toBeInstanceOf(Promise);
done();
});

it('should retrieve ttl for a given key', (done) => {
redisCache.set('foo', 'bar', () => {
redisCache.ttl('foo', (err, ttl) => {
Expand Down

0 comments on commit 809fec2

Please sign in to comment.