mirror of
https://github.com/vale981/apollo-server
synced 2025-03-06 02:01:40 -05:00
Use dataloader to batch gets in apollo-server-redis
This commit is contained in:
parent
19dbc36436
commit
ca26c40ef5
2 changed files with 10 additions and 2 deletions
|
@ -25,6 +25,7 @@
|
|||
"dependencies": {
|
||||
"apollo-server-caching": "0.1.0-rc.11",
|
||||
"apollo-server-env": "2.0.0-rc.11",
|
||||
"dataloader": "^1.4.0",
|
||||
"redis": "^2.8.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import { KeyValueCache } from 'apollo-server-caching';
|
||||
import * as Redis from 'redis';
|
||||
import { promisify } from 'util';
|
||||
import * as DataLoader from 'dataloader';
|
||||
|
||||
export class RedisCache implements KeyValueCache {
|
||||
readonly client;
|
||||
|
@ -8,10 +9,16 @@ export class RedisCache implements KeyValueCache {
|
|||
ttl: 300,
|
||||
};
|
||||
|
||||
private loader: DataLoader<string, string>;
|
||||
|
||||
constructor(options: Redis.ClientOpts) {
|
||||
this.client = Redis.createClient(options);
|
||||
this.loader = new DataLoader(keys => this.client.mget(keys), {
|
||||
cache: false,
|
||||
});
|
||||
|
||||
// promisify client calls for convenience
|
||||
this.client.get = promisify(this.client.get).bind(this.client);
|
||||
this.client.mget = promisify(this.client.mget).bind(this.client);
|
||||
this.client.set = promisify(this.client.set).bind(this.client);
|
||||
this.client.flushdb = promisify(this.client.flushdb).bind(this.client);
|
||||
this.client.quit = promisify(this.client.quit).bind(this.client);
|
||||
|
@ -27,7 +34,7 @@ export class RedisCache implements KeyValueCache {
|
|||
}
|
||||
|
||||
async get(key: string): Promise<string | undefined> {
|
||||
const reply = await this.client.get(key);
|
||||
const reply = await this.loader.load(key);
|
||||
// reply is null if key is not found
|
||||
if (reply !== null) {
|
||||
return reply;
|
||||
|
|
Loading…
Add table
Reference in a new issue