Dragonfly Cloud announces new enterprise security features - learn more

Dragonfly

Node Redis Bulk Get (Detailed Guide w/ Code Examples)

Use Case(s)

In Node.js applications, we often need to get multiple keys from a Redis store at once. This can be accomplished using the mget command, which stands for 'Multi Get'. It allows you to retrieve the values of all specified keys.

Code Examples

  1. Basic Usage:
const redis = require('redis');
const client = redis.createClient();

client.mget(['key1', 'key2', 'key3'], function(err, reply) {
    console.log(reply); // will print the values of key1, key2 and key3
});

This example shows how you can use mget to fetch the values of multiple keys ('key1', 'key2', 'key3') at once.

  1. With Promise:
const { promisify } = require('util');
const redis = require('redis');
const client = redis.createClient();
const getAsync = promisify(client.mget).bind(client);

getAsync(['key1', 'key2', 'key3']).then(console.log).catch(console.error);

In this example, we're wrapping the mget call with Node's built-in promisify utility, allowing us to work with Promises instead of callbacks.

Best Practices

Common Mistakes

FAQs

  1. Can I use wildcard characters with mget?
    No. The Redis mget command does not support wildcards. If you need to get all keys matching a pattern, you need to first use the keys command to get the keys and then use mget.
  2. What happens if some of the keys do not exist?
    If some of the keys don't exist, Redis will return null for those keys.

Was this content helpful?

Similar Code Examples

White Paper

Free System Design on AWS E-Book

Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

Free System Design on AWS E-Book

Switch & save up to 80% 

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement. Instantly experience up to a 25X boost in performance and 80% reduction in cost