基于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);
        
    }
相关推荐
PC2005-cloud24 分钟前
Redis学习笔记:Cluster 集群读写分离实战,Lettuce + Spring Boot 从配置到验证
redis·笔记·学习
Crazy________1 小时前
Redis02:库切换、监控与安全配置
linux·redis·云原生·mybatis
海上小飞龙1 小时前
Redis 持久化:RDB 与 AOF
数据库·redis·缓存
hold?fish:palm1 小时前
redis中AOF 重写机制解析
数据库·c++·redis
2401_894915532 小时前
GEO 定位优化源码搭建常见报错排查:数据库、伪静态、接口调试
java·数据库·网络协议·tcp/ip·spring·unity
math_hongfan3 小时前
鸿蒙存储碎片高级清理机制:数据库Vacuum/文件碎片整理/缓存过期清理/主动空间回收策略
jvm·数据库·学习·缓存·华为·harmonyos·鸿蒙
范什么特西4 小时前
知识总结04(redis)
数据库·redis·缓存
wangjialelele4 小时前
Spring Cloud 全体系学习笔记(REST、Nacos、Feign、Gateway)
java·spring boot·笔记·学习·spring·spring cloud·gateway
玖石书4 小时前
ASP.NET Core迁移Spring对照系列:文件系统操作篇
spring·asp.net
xqqxqxxq4 小时前
吃透 Spring 高频注解(包含SpringMVC Spring Boot)
java·spring boot·spring