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.
Redis Native Command Syntax
Section titled “Redis Native Command Syntax”MGET key [key ...]Parameter Description
- key: One or more keys to get values
Detailed Explanation
Section titled “Detailed Explanation”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.
Redisun Usage
Section titled “Redisun Usage”In redisun, the MGET command is implemented through the MGetCommand class and the mget method in the Redisun class.
Basic Usage
Section titled “Basic Usage”Redisun redisun = Redisun.create(options -> { options.setAddress("redis://127.0.0.1:6379");});
// Prepare key listList<String> keys = Arrays.asList("key1", "key2", "key3");
// Get values of multiple keysList<String> values = redisun.mget(keys);for (int i = 0; i < values.size(); i++) { System.out.println(keys.get(i) + " = " + values.get(i));}
// Asynchronous versionCompletableFuture<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)); }});- If a key does not exist, the value at the corresponding position in the return list is null
- MGET is an atomic operation, either all execute successfully or all fail
- Better performance than calling the GET command multiple times, as it reduces network round trips