基于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);
        
    }
相关推荐
JavaGuide5 小时前
Claude Opus 4.6 真的用不起了!我换成了国产 M2.5,实测真香!!
java·spring·ai·claude code
玹外之音8 小时前
Spring AI MCP 实战:将你的服务升级为 AI 可调用的智能工具
spring·ai编程
来一斤小鲜肉9 小时前
Spring AI入门:第一个AI应用跑起来
spring·ai编程
NE_STOP10 小时前
springMVC-常见视图组件与RESTFul编程风格
spring
what丶k1 天前
Spring AI 多模态开发全解析:从入门到企业级落地
后端·spring·ai编程
NE_STOP1 天前
springMVC-获取前端请求的数据与三个作用域
spring
莫寒清1 天前
Spring MVC:@PathVariable 注解详解
java·spring·mvc
知我Deja_Vu1 天前
redisCommonHelper.generateCode(“GROUP“),Redis 生成码方法
数据库·redis·缓存
-大头.1 天前
从 0 开始理解 Spring 的核心思想 —— IoC 和 DI(1)
spring
Charlie_lll1 天前
Redis脑裂问题处理——基于min-replicas-to-write配置
redis·后端