question
Use the opsForValue() method of the RedisTemplate object to get the value in Redis but cannot get it
detailed question
The author's code is as follows
1 Use the set() method of the ValueOperations object to store a key-value pair in Redis
valueOperations.set("order:" + user.getId() + ":" + goods.getId(), seckillOrder);
2 Use the opsForValue() method of the RedisTemplate object to get the value in Redis
redisTemplate.opsForValue().get("order:" + user.getId() + ":" + goodsId);
But breakpoint debugging
solution
Make sure the key used by the set() method is the same as the key used by the get() method
In my case, that is
valueOperations.set("order:" + user.getId() + ":" + goods.getId(), seckillOrder);
redisTemplate.opsForValue().get("order:" + user.getId() + ":" + goodsId);
or
valueOperations.set("order" + user.getId() + ":" + goods.getId(), seckillOrder);
redisTemplate.opsForValue().get("order" + user.getId() + ":" + goodsId);
problem causes
Unable to fetch due to key inconsistency. The key stored by the author is "order:" + user.getId() + ":" + goods.getId()
used when obtaining
"order" + user.getId() + ":" + goodsId
, which naturally does not exist, so it cannot be obtained
solve the cause
Modify the key naming to be consistent, and you can get the object.
It’s not easy
to be original, please indicate the source
if it’s helpful to you, don’t forget to like it and support it