Skip to content

Commit 5d9382a

Browse files
Brian DeteringBrian Detering
authored andcommitted
fix for cacheKeyFn invoked twice on loadMany #6
1 parent c8a96ce commit 5d9382a

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

index.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,7 @@ module.exports = fig => {
103103
this.keySpace = ks;
104104
this.loader = new DataLoader(
105105
keys =>
106-
rMGet(
107-
this.keySpace,
108-
_.map(keys, this.opt.cacheKeyFn),
109-
this.opt
110-
).then(results =>
106+
rMGet(this.keySpace, keys, this.opt).then(results =>
111107
Promise.map(results, (v, i) => {
112108
if (v === '') {
113109
return Promise.resolve(null);

test/test.js

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@ const chai = require('chai');
44
chai.use(require('chai-as-promised'));
55
const { expect } = chai;
66
const sinon = require('sinon');
7-
// const redis = require('redis').createClient();
87
const DataLoader = require('dataloader');
98
const createRedisDataLoader = require('../index');
109

11-
// const RedisDataLoader = require('../index.js')({ redis });
12-
1310
module.exports = ({ name, redis }) => {
1411
const RedisDataLoader = createRedisDataLoader({ redis });
1512

@@ -230,6 +227,41 @@ module.exports = ({ name, redis }) => {
230227

231228
it('should require array', () =>
232229
expect(this.loader.loadMany()).to.be.rejectedWith(TypeError));
230+
231+
it('should handle custom cacheKeyFn', () => {
232+
const loader = new RedisDataLoader(this.keySpace, this.userLoader(), {
233+
cacheKeyFn: key => `foo-${key}`,
234+
});
235+
236+
loader.loadMany(['json', 'null']).then(results => {
237+
expect(results).to.deep.equal([this.data.json, this.data.null]);
238+
});
239+
});
240+
241+
it('should use local cache on second load when using custom cacheKeyFn', () => {
242+
this.stubs.redisMGet = sinon.stub(redis, 'mget', (keys, cb) => {
243+
cb(null, [JSON.stringify(this.data.json)]);
244+
});
245+
246+
const loader = new RedisDataLoader(this.keySpace, this.userLoader(), {
247+
cacheKeyFn: key => `foo-${key}`,
248+
});
249+
250+
return loader
251+
.loadMany(['json'])
252+
.then(data => {
253+
expect(this.loadFn.callCount).to.equal(0);
254+
expect(this.stubs.redisMGet.args[0][0]).to.deep.equal([
255+
'key-space:foo-json',
256+
]);
257+
expect(this.stubs.redisMGet.callCount).to.equal(1);
258+
return loader.loadMany(['json']);
259+
})
260+
.then(data => {
261+
expect(this.loadFn.callCount).to.equal(0);
262+
expect(this.stubs.redisMGet.callCount).to.equal(1);
263+
});
264+
});
233265
});
234266

235267
describe('prime', () => {

0 commit comments

Comments
 (0)