Skip to content

Commit a179da7

Browse files
Brian DeteringBrian Detering
authored andcommitted
clearLocal, clearAllLocal
1 parent 264b0df commit a179da7

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ with a few differences. Read through the [Facebook Dataloader documentation](htt
5050
- `prime` will always overwrite the cache. Facebook Dataloader will only write to
5151
its cache if a value is not already present. Prime is asyncronous and returns a Promise.
5252
- dataloader results must be either `null` or a JSON object.
53+
- two functions: `clearLocal(key)` and `clearAllLocal()` allow you to clear the local cache only.
5354

5455
### Instantiation
5556

index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,14 @@ module.exports = fig => {
137137
rDel(this.keySpace, key).then(() => this.loader.clear(key)) :
138138
Q.reject(new TypeError('key parameter is required'));
139139
}
140+
141+
clearAllLocal() {
142+
return Q(this.loader.clearAll());
143+
}
144+
145+
clearLocal(key) {
146+
return Q(this.loader.clear(key));
147+
}
140148
};
141149
};
142150

test.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ describe('redis-dataloader', () => {
1414
key, (err, resp) => err ? reject(err) : resolve(resp)
1515
));
1616

17+
this.rSet = (k, v) => Q.Promise((resolve, reject) => redis.set(
18+
k, v, (err, resp) => err ? reject(err) : resolve(resp)
19+
));
20+
1721
this.keySpace = 'key-space';
1822
this.data = {
1923
json: { foo: 'bar' },
@@ -253,4 +257,40 @@ describe('redis-dataloader', () => {
253257
}).done();
254258
});
255259
});
260+
261+
describe('clearAllLocal', () => {
262+
it('should clear all local in-memory cache', done => {
263+
this.loader.loadMany(['json', 'null'])
264+
.then(() => this.loader.clearAllLocal())
265+
.then(() => this.rSet(
266+
`${this.keySpace}:json`, JSON.stringify({ new: 'valeo' })
267+
))
268+
.then(() => this.rSet(
269+
`${this.keySpace}:null`, JSON.stringify({ foo: 'bar' })
270+
))
271+
.then(() => this.loader.loadMany(['null', 'json']))
272+
.then(data => {
273+
expect(data).to.deep.equal([{ foo: 'bar' }, { new: 'valeo' }]);
274+
done();
275+
}).done();
276+
});
277+
});
278+
279+
describe('clearLocal', () => {
280+
it('should clear local cache for a specific key', done => {
281+
this.loader.loadMany(['json', 'null'])
282+
.then(() => this.loader.clearLocal('json'))
283+
.then(() => this.rSet(
284+
`${this.keySpace}:json`, JSON.stringify({ new: 'valeo' })
285+
))
286+
.then(() => this.rSet(
287+
`${this.keySpace}:null`, JSON.stringify({ foo: 'bar' })
288+
))
289+
.then(() => this.loader.loadMany(['null', 'json']))
290+
.then(data => {
291+
expect(data).to.deep.equal([null, { new: 'valeo' }]);
292+
done();
293+
}).done();
294+
});
295+
});
256296
});

0 commit comments

Comments
 (0)