基于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);
        
    }
相关推荐
杨凯凡15 分钟前
【021】反射与注解:Spring 里背后的影子
java·后端·spring
riNt PTIP17 分钟前
SpringBoot创建动态定时任务的几种方式
java·spring boot·spring
Lyyaoo.1 小时前
Redis基础
数据库·redis·缓存
invicinble1 小时前
spirng的bean的生命周期,以及为什么这么设计
spring
yuweiade2 小时前
Spring Boot 整合 Redis 步骤详解
spring boot·redis·bootstrap
三水不滴3 小时前
SpringAI + SpringDoc + Knife4j 构建企业级智能问卷系统
经验分享·spring boot·笔记·后端·spring
JH30735 小时前
RedLock-红锁
java·redis
一嘴一个橘子5 小时前
redis 启动
redis
if else5 小时前
Redis 哨兵集群部署方案
数据库·redis
rannn_1116 小时前
【Redis|原理篇2】Redis网络模型、通信协议、内存回收
java·网络·redis·后端·缓存