is Redis-Template having method which support HMGET commands. which method I can use from Redis-Template to run this command "HMGET KEY field1 field2"?
Also How do we run the same command through Redis-Template?, So without going for predefined method, I directly want to run the above/any command through a method , do we have support for this.
In Redis-Template there is method called opsForHash()
for HASH
data structures, such methods are recommended way of working with Redis commands.
However if you really want yo use "raw" commands you can use connection.execute()
, here is an example:
@Component
public class RedisDemo {
private final RedisTemplate<String, String> redisTemplate;
@Autowired
public RedisDemo(RedisTemplate<String, String> redisTemplate) {
this.redisTemplate = redisTemplate;
}
// This method can execute any command
public Object executeCommand(String command, byte[]... parts) {
return redisTemplate.execute(connection ->
connection.execute(command, parts),
true
);
}
}
Calling this method:
redisDemo.executeCommand(
"HMGET",
"test".getBytes(),
"field1".getBytes(),
"field1".getBytes()
);
Just for comparation here is example with opsForHash()
:
public List<Object> callHMGet() {
return redisTemplate.opsForHash().multiGet(
"test",
List.of("field1", "field2")
);
}