基于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);
        
    }
相关推荐
c***87197 分钟前
Nginx 缓存清理
运维·nginx·缓存
e***877015 分钟前
Redis四种模式在Spring Boot框架下的配置
spring boot·redis·bootstrap
zs宝来了19 分钟前
Redis的String 底层实现
数据库·redis·缓存
旷野说29 分钟前
用 Redis + Lua 守住打赏原子性:我在单体系统中的微观实践(续)
redis·junit·lua
野生技术架构师37 分钟前
Java 经典面试题汇总:多线程 +spring+JVM 调优 + 分布式 +redis+ 算法
java·jvm·spring
v***598342 分钟前
springboot中配置logback-spring.xml
spring boot·spring·logback
h***381842 分钟前
使用 Logback 的最佳实践:`logback.xml` 与 `logback-spring.xml` 的区别与用法
xml·spring·logback
李宥小哥43 分钟前
Redis16-实践-优惠秒杀2
缓存·中间件
永不停歇的蜗牛43 分钟前
解决方法:在本地电脑安装的Centos虚拟机上启动redis服务,使用本地电脑客户端无法连接到redis。
linux·redis·centos
z***565643 分钟前
GO 快速升级Go版本
开发语言·redis·golang