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.
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.
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.
null
values for those keys, which may not be the expected behavior in your application.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
.
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.
Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.