Redis hash type

✒️ 2026-07-01 21:06 내용 수정


실습 참고 자료


Hash Type


hset, hget 메서드

package myproject.redis.lettuce.hash;  
  
import org.junit.jupiter.api.Test;  
  
public class RedisLettuceHash {  
    @Test  
    public void hashTest() {  
        CommandAction action = redisCommands -> {  
            String key = "lettuce:hash";  
  
            // hset, hget
            // 필드명 지정
            String field = "roleCode";  
  
			// hash 타입으로 field명에 value 저장
            redisCommands.hset(key, field, "A001");  
            
            String hgetResult = redisCommands.hget(key, field);  
            System.out.println("hget = " + hgetResult);  
        };  
        CommandTemplate.commandAction(action);  
    }  
}

springboot_redis 15.png


hmset, hmget 메서드

@Test  
public void hashTest() {  
    CommandAction action = redisCommands -> {  
        String key = "lettuce:hash";  
  
		// hset, hget
		// 필드명 지정
		String field = "roleCode";  

		// hash 타입으로 field명에 value 저장
		redisCommands.hset(key, field, "A001");  
		
		String hgetResult = redisCommands.hget(key, field);  
		System.out.println("hget = " + hgetResult);  
  
        // hmset, hmget  
        Map<String, String> map = new HashMap<>();  
        map.put("roleCode", "A001");  
        map.put("dvsnCode", "B123");  
        map.put("age", "27");  
        redisCommands.hmset(key, map);  
  
        List<KeyValue<String, String>> hmgetResult =  
                redisCommands.hmget(key, map.keySet().toArray(new String[0]));  
        hmgetResult.stream().forEach(m -> {  
            System.out.println("field = " + m.getKey() + " | value = " + m.getValue());  
        });  
  
        // hgetall  
        Map<String, String> hgetallResult = redisCommands.hgetall(key);  
        System.out.println(hgetallResult);  
    };  
    CommandTemplate.commandAction(action);  
}

springboot_redis 16.png