基于Redis添加商铺查询缓存

文章目录

1.流程图

2.实现逻辑

Service层

java 复制代码
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);
        
    }
相关推荐
无敌的牛7 小时前
redis学习过程
数据库·redis·学习
摇滚侠8 小时前
SpringMVC 入门到实战 文件上传 75-77
java·后端·spring·maven·intellij-idea
swordbob10 小时前
Redis 3 大问题 + 5 大扩展问题
redis
AOwhisky11 小时前
Redis 学习笔记(第一期):概述、安装配置与核心理论
运维·数据库·redis·笔记·学习·云计算
AOwhisky12 小时前
Redis 学习笔记(第四期):高可用与集群(哨兵 + Cluster + 容器化)
linux·运维·数据库·redis·笔记·学习·缓存
MXsoft61813 小时前
**断网续传与本地缓存:弱网环境下的监控数据保障方案**
运维·缓存·自动化
xingyuzhisuan14 小时前
缓存命中率提升方案:从 30% 优化至 82% 全流程优化记录
java·开发语言·缓存·ai
Konwledging14 小时前
Cache Incoherent(缓存不一致)
缓存
IT策士15 小时前
Redis 从入门到精通:事务与 Lua 脚本
redis·junit·lua