基于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);
        
    }
相关推荐
我是无敌小恐龙2 小时前
Java SE 零基础入门Day01 超详细笔记(开发前言+环境搭建+基础语法)
java·开发语言·人工智能·opencv·spring·机器学习
SPC的存折4 小时前
D在 Alpine 容器中手动搭建 Discuz 全攻略(包含镜像一键部署脚本,可直接用)
linux·数据库·mysql·缓存
一只小白0004 小时前
Redis 常用命令总结
数据库·redis·缓存
云烟成雨TD5 小时前
Spring AI Alibaba 1.x 系列【28】Nacos Skill 管理中心功能说明
java·人工智能·spring
Han.miracle5 小时前
Redis 全套笔记:基础 API + 三大架构 + 缓存三大问题
java·windows·redis
QC·Rex5 小时前
Spring AI MCP Apps 实战:打造聊天与富 UI 融合的智能化应用
人工智能·spring·ui·spring ai·mcp
Predestination王瀞潞5 小时前
Java EE3-我独自整合(第七章:Spring AOP 通知类型)
python·spring·java-ee
曹牧5 小时前
Spring :component-scan
java·后端·spring
风吹迎面入袖凉5 小时前
【Redis】Redis缓存击穿
数据库·redis·缓存
曹牧6 小时前
Spring:@RequestMapping
java·后端·spring