Skip to content

Latest commit

 

History

History
32 lines (26 loc) · 819 Bytes

Redis.md

File metadata and controls

32 lines (26 loc) · 819 Bytes

Using DataLoader with Redis

Redis is a very simple key-value store which provides the batch load method MGET which makes it very well suited for use with DataLoader.

Here we build an example Redis DataLoader using node_redis.

const DataLoader = require('dataloader');
const redis = require('redis');

const client = redis.createClient();

const redisLoader = new DataLoader(
  keys =>
    new Promise((resolve, reject) => {
      client.mget(keys, (error, results) => {
        if (error) {
          return reject(error);
        }
        resolve(
          results.map((result, index) =>
            result !== null ? result : new Error(`No key: ${keys[index]}`),
          ),
        );
      });
    }),
);