Julio
Julio

Reputation: 439

Redis spring boot how can I see values from command line

I'm working with spring boot and Redis, I have this simple structure

public class Redis {    
    @Id
    private String key;

    private Integer value1;

    private Integer value2;
}

@Repository
public interface RedisRepository extends CrudRepository<Redis, String> {
}


redisRepository.save(new Redis("myKey", 1, 2));

127.0.0.7:6379> KEYS *
1) "myKey"
127.0.0.7:6379> type "myKey"
set

I don't see any values, how can I see the values from command line, in this case I want to see 1 and 2. Thanks

Upvotes: 1

Views: 2053

Answers (1)

Shawrup
Shawrup

Reputation: 2734

If you use RedisRepository, spring does not use a simple key, value pattern to store object data in redis. It uses the hash operation of redis. For example, I have a model RedisModel.java

@Data
@AllArgsConstructor
@RedisHash("redis_model")
public class RedisModel {
    @Id
    String key;
    Integer value1;
    Integer value2;
}

Also a repository RedisRepository.java

@Repository
public interface RedisRepository extends CrudRepository<RedisModel, String> {
}

I saved a RedisModel with RedisRepository like this

redisRepository.save(new RedisModel("key",1,2));

But when I monitor the log of redis I see that

1603128368.772046 [0 127.0.0.1:37776] "DEL" "redis_model:key"
1603128368.781117 [0 127.0.0.1:37776] "HMSET" "redis_model:key" "_class" "com.bimurto.springdataredis.RedisModel" "key" "key" "value1" "1" "value2" "2"
1603128368.782503 [0 127.0.0.1:37776] "SADD" "redis_model" "key"

So if you want to see the values in redis command line you have to use Hash Operation Commands of redis. If you want to get all values of your object, use HGETALL. For example

127.0.0.1:6379> HGETALL redis_model:key
1) "_class"
2) "com.bimurto.springdataredis.RedisModel"
3) "key"
4) "key"
5) "value1"
6) "1"
7) "value2"
8) "2"

if you want the value of a single key , then use HMGET like

127.0.0.1:6379>HMGET redis_model:key value1
1) "1"

Upvotes: 2

Related Questions