DataBase

[Redis] WRONGTYPE Operation against a key holding the wrong kind of value

winwin-k9 2023. 8. 28. 00:19

WRONGTYPE Operation against a key holding the wrong kind of value

 

레디스는 기본적으로 5가지 타입을 제공하고 있으며 데이터베이스에 한번 타입이 결정된 상태에서 해당 타입과 상관없는 명령을 수행하려고 할때 위와 같은 에러 메시지가 출력된다. 꼭 수행해야되는 명령이라면 기존의 키를 지우면 된다.

 

Redis support 5 types of data types. You need to know what type of value that key maps to, as for each data type, the command to retrieve it is different.

Here are the commands to retrieve key value:

  • if value is of type string -> GET <key>
  • if value is of type hash -> HGETALL <key>
  • if value is of type lists -> lrange <key> <start> <end>
  • if value is of type sets -> smembers <key>
  • if value is of type sorted sets -> ZRANGEBYSCORE <key> <min> <max>

command to check the type of value a key mapping to:

  • type <key>

 

나의 경우는 Entity를 다음과 같이 설정했다.

 

@Getter
@AllArgsConstructor
@NoArgsConstructor
@Builder
@RedisHash(value = "food")
public class Food {

    @Id
    private long id;

    private String name;
    private double kcal;
    private double carbohydrate;
    private double protein;
    private double fat;
}

 

key는 다음과 같이 저장되어 있다.

 

 

food는 Hash형태로 저장되어 있다. 

따라서 food를 조회하고 싶을때는 

 

 HGETALL food:0

이와 같이 조회를 해야한다.

728x90