Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add mget support #29

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 37 additions & 10 deletions dist/index.js
Expand Up @@ -67,17 +67,37 @@ const redisStore = (...args) => {
redisCache.get(key, handleResponse(cb, { parse: true }));
})
);

self.del = (key, options, cb) => {
if (typeof options === 'function') {
cb = options;
}

redisCache.del(key, handleResponse(cb));
};
self.mget = (keys, options, cb) => (
new Promise((resolve, reject) => {
if (typeof options === 'function') {
cb = options;
}

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

redisCache.mget(keys, handleResponse(cb, { parse: true }));
})
);

self.del = (key, options, cb) => (
new Promise((resolve, reject) => {
if (typeof options === 'function') {
cb = options;
}

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

redisCache.del(key, handleResponse(cb));
})
);

self.reset = cb => redisCache.flushdb(handleResponse(cb));

self.keys = (pattern, cb) => (
new Promise((resolve, reject) => {
if (typeof pattern === 'function') {
Expand All @@ -97,19 +117,26 @@ const redisStore = (...args) => {

return self;
};

function handleResponse(cb, opts = {}) {
return (err, result) => {
if (err) {
return cb && cb(err);
}

if (opts.parse) {
const isMultiple = Array.isArray(result);
if (!isMultiple) {
result = [result];
}

try {
result = JSON.parse(result);
result = result.map((_result) => JSON.parse(_result));
} catch (e) {
return cb && cb(e);
}

result = isMultiple ? result : result[0];
}

return cb && cb(null, result);
Expand Down
59 changes: 50 additions & 9 deletions index.js
@@ -1,5 +1,7 @@
const Redis = require('ioredis');

const isObject = (value) => value instanceof Object && value.constructor === Object;

const redisStore = (...args) => {
let redisCache = null;

Expand Down Expand Up @@ -65,17 +67,49 @@ const redisStore = (...args) => {
redisCache.get(key, handleResponse(cb, { parse: true }));
})
);

self.del = (key, options, cb) => {
if (typeof options === 'function') {
cb = options;

self.mget = function () {
var args = Array.prototype.slice.apply(arguments);
var cb;
var options = {};

if (typeof args[args.length - 1] === 'function') {
cb = args.pop();
}

redisCache.del(key, handleResponse(cb));
};
if (isObject(args[args.length - 1])) {
options = args.pop();
}

if (Array.isArray(args[0])) {
args = args[0];
}

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

redisCache.mget(args, handleResponse(cb, { parse: true }));
});
}

self.del = (key, options, cb) => (
new Promise((resolve, reject) => {
if (typeof options === 'function') {
cb = options;
}

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

redisCache.del(key, handleResponse(cb));
})
);

self.reset = cb => redisCache.flushdb(handleResponse(cb));

self.keys = (pattern, cb) => (
new Promise((resolve, reject) => {
if (typeof pattern === 'function') {
Expand All @@ -95,19 +129,26 @@ const redisStore = (...args) => {

return self;
};

function handleResponse(cb, opts = {}) {
return (err, result) => {
if (err) {
return cb && cb(err);
}

if (opts.parse) {
const isMultiple = Array.isArray(result);
if (!isMultiple) {
result = [result];
}

try {
result = JSON.parse(result);
result = result.map((_result) => JSON.parse(_result));
} catch (e) {
return cb && cb(e);
}

result = isMultiple ? result : result[0];
}

return cb && cb(null, result);
Expand Down
71 changes: 71 additions & 0 deletions test/index.test.js
Expand Up @@ -239,6 +239,55 @@ redisCache.store.getClient().end(true);
});
});

it('should retrieve multiple values', (done) => {
redisCache.set('foo', 'bar', () => {
redisCache.set('foo1', 'bar1', () => {
redisCache.mget(['foo', 'foo1', 'foo2'], (err, result) => {
expect(err).toEqual(null);
expect(result).toEqual(['bar', 'bar1', null]);
done();
});
});
});
});

it('should retrieve multiple values with options', (done) => {
redisCache.set('foo', 'bar', () => {
redisCache.set('foo1', 'bar1', () => {
redisCache.mget(['foo', 'foo1', 'foo2'], {}, (err, result) => {
expect(err).toEqual(null);
expect(result).toEqual(['bar', 'bar1', null]);
done();
})
.catch((err) => done(err))
});
});
});

it('should retrieve multiple values with options and varargs', (done) => {
redisCache.set('foo', 'bar', () => {
redisCache.set('foo1', 'bar1', () => {
redisCache.mget('foo', 'foo1', 'foo2', {}, (err, result) => {
expect(err).toEqual(null);
expect(result).toEqual(['bar', 'bar1', null]);
done();
})
.catch((err) => done(err))
});
});
});

it('should retrieve multiple values promise', (done) => {
redisCache.set('foo', 'bar', () => {
redisCache.set('foo1', 'bar1', () => {
redisCache.mget(['foo', 'foo1', 'foo2']).then((result) => {
expect(result).toEqual(['bar', 'bar1', null]);
done();
});
});
});
});

it('should retrieve a value for a given key if options provided', (done) => {
const value = 'bar';
redisCache.set('foo', value, () => {
Expand Down Expand Up @@ -268,6 +317,28 @@ redisCache.store.getClient().end(true);
});

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

it('should resolve promise on success', (done) => {
redisCache.set('foo', 'bar')
.then(() => redisCache.del('foo'))
.then(() => redisCache.get('foo'))
.then((result) => {
expect(result).toEqual(null);
done();
});
});

it('should reject promise on error', (done) => {
redisCache.store.getClient().end(true);
redisCache.del('foo')
.then(() => done(new Error('Should reject')))
.catch(() => done())
});

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