1.流程图
2.实现逻辑
- public Result queryById(Long id) {
- // 1.从redis中查询店铺缓存
- String shopJson = stringRedisTemplate.opsForValue().get("cache:shop:"+id);
- // 2.判断缓存是否存在
- if (StrUtil.isNotBlank(shopJson)) {
- Shop shop = JSONUtil.toBean(shopJson, Shop.class);
- return Result.ok(shop);
- }
- // 3.不存在,根据id查询数据库
- Shop shop = getById(id); // mybatisplus功能
- // 4.不存在,返回错误
- if (shop == null) {
- return Result.fail("店铺数据不存在");
- }
- // 存在,写入redis
- stringRedisTemplate.opsForValue().set("cache:shop:"+id, JSONUtil.toJsonStr(shop));
- return Result.ok(shop);
- }
复制代码 到此这篇关于Redis添加商铺查询缓存功能实现的文章就介绍到这了,更多相关Redis商铺查询缓存内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
来源:https://www.jb51.net/database/338208ni2.htm
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |