Skip to content

MGET

Return the values of all (one or more) given keys.

If some keys do not exist among the given keys, those keys return the special value nil. Therefore, this command never fails.

Terminal window
MGET key [key ...]

Parameter Description

  • key: One or more keys to get values

The MGET command is an atomic operation used to get the values of multiple keys simultaneously. Compared to calling the GET command multiple times, MGET can reduce the number of network round trips and improve performance.

In redisun, the MGET command is implemented through the MGetCommand class and the mget method in the Redisun class.

Redisun redisun = Redisun.create(options -> {
options.setAddress("redis://127.0.0.1:6379");
});
// Prepare key list
List<String> keys = Arrays.asList("key1", "key2", "key3");
// Get values of multiple keys
List<String> values = redisun.mget(keys);
for (int i = 0; i < values.size(); i++) {
System.out.println(keys.get(i) + " = " + values.get(i));
}
// Asynchronous version
CompletableFuture<List<String>> future = redisun.asyncMget(keys);
future.thenAccept(result -> {
for (int i = 0; i < result.size(); i++) {
System.out.println(keys.get(i) + " = " + result.get(i));
}
});
  1. If a key does not exist, the value at the corresponding position in the return list is null
  2. MGET is an atomic operation, either all execute successfully or all fail
  3. Better performance than calling the GET command multiple times, as it reduces network round trips