基于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);
        
    }
相关推荐
记得开心一点嘛14 小时前
手搓Springboot
java·spring boot·spring
异常驯兽师17 小时前
Spring 中处理 HTTP 请求参数注解全解析
java·spring·http
ouou061717 小时前
企业级NoSql数据库Redis集群
数据库·redis·nosql
蒋星熠19 小时前
破壁者指南:内网穿透技术的深度解构与实战方法
网络·数据库·redis·python·websocket·网络协议·udp
纪莫19 小时前
技术面:Spring (bean的生命周期、创建方式、注入方式、作用域)
java·spring·java面试⑧股
七夜zippoe20 小时前
缓存三大劫攻防战:穿透、击穿、雪崩的Java实战防御体系(一)
java·开发语言·缓存
爱干饭的boy21 小时前
手写Spring底层机制的实现【初始化IOC容器+依赖注入+BeanPostProcesson机制+AOP】
java·数据结构·后端·算法·spring
躲在云朵里`1 天前
Redis深度解析:核心数据结构、线程模型与高频面试题
数据结构·数据库·redis
恣艺1 天前
Redis是什么?一篇讲透它的定位、特点与应用场景
数据库·redis·缓存
AD钙奶-lalala1 天前
Spring Initializr(或者 IDEA 里新建 Spring Boot 项目)时 Dependencies 的选择
spring boot·spring·intellij-idea