My requirement is that in redis db, keys should be stored as strings and values as byte array.
And I am able to achieve the same in java using lettuce’s RedisCodec. But when I view the value through reds-cli it appears to be in original string itself rather than the byte format. Following is the simple code I am trying with.
RedisURI redisURI = RedisURI.builder().withHost("localhost")
.withPort(6379)
.build();
RedisClient redisClient = RedisClient.create(redisURI);
RedisCommands<String, byte[]> redisCommands = redisClient.connect(RedisCodec.of(new StringCodec(), new ByteArrayCodec())).sync();
redisCommands.set("key", "value".getBytes(StandardCharsets.UTF_8));
System.out.println(redisCommands.get("key"));
Console O/p : [B@3bd7f8dc
redis-cli o/p:
127.0.0.1:6379> get key
"value"
I am using lettuce as redis client. I am unable to understand why the value I obtained from redis-cli is in string itself rather than the byte format.
Any help is appreciated. Thanks in advance.
3
Answers
I believe that below answer helps you to understand reading byte value from redis-cli.
How to read Redis binary values in redis console
Here is also detail explaniton of redis-cli. I hope it makes sense.
https://redis.io/docs/manual/cli/
The second argument from
RedisCodec
is how you will transmit the value data to Redis Server, which in your case is throughByteArrayCodec
, once it is retrieved usingget
is serialized back to the original form.What I use to read byte arrays from redis is something like this: