public Result queryById(Long id) {`
if (id == null) {
return Result.fail("id为空");
}
String key = CACHE_SHOP_KEY + id;
String shopJson = stringRedisTemplate.opsForValue().get(key);
if (StrUtil.isNotBlank(shopJson)) {
return Result.ok(shopJson);
}
Shop shop = getById(id);
if (shop == null) {
return Result.fail("商户不存在");
}
stringRedisTemplate.opsForValue().set(key, JSONUtil.toJsonStr(shop), Duration.ofMinutes(CACHE_SHOP_TTL));
return Result.ok(shop);
}
如果从Redis中取出Json字符串后直接返回,那么前端接收到的数据是这样的
因此需要先转成对象再返回
public Result queryById(Long id) {
if (id == null) {
return Result.fail("id为空");
}
String key = CACHE_SHOP_KEY + id;
String shopJson = stringRedisTemplate.opsForValue().get(key);
if (StrUtil.isNotBlank(shopJson)) {
Shop shopBean = JSONUtil.toBean(shopJson, Shop.class);
return Result.ok(shopBean);
}
Shop shop = getById(id);
if (shop == null) {
return Result.fail("商户不存在");
}
stringRedisTemplate.opsForValue().set(key, JSONUtil.toJsonStr(shop), Duration.ofMinutes(CACHE_SHOP_TTL));
return Result.ok(shop);
}
那么前端接收到的数据是这样的
评论区