基于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 小时前
SpringMVC练习:加法计算器与登录
java·spring boot·spring·mvc
程序员小凯16 小时前
Spring MVC 多租户架构与数据隔离教程
spring·架构·mvc
Knight_AL16 小时前
代理模式 vs AOP:支付服务中的日志增强实践(含执行顺序详解)
spring·代理模式
洲覆17 小时前
基于 clangd 搭建 Redis 6.2 源码阅读与调试环境
开发语言·数据库·redis·缓存
凯子坚持 c17 小时前
Redis 事务深度解析:从基础到实践
数据库·redis·缓存
Zz_waiting.17 小时前
Spring Cloud 概述
后端·spring·spring cloud
西蓝花MQ18 小时前
Spring Cloud微服务篇面试题总结
spring·spring cloud·微服务
zl9798991 天前
Redis-持久化之AOF
redis
xyy20251 天前
Spring事务的传播方式
java·数据库·spring
非凡的世界1 天前
Thinkphp8 Redis队列与消息队列topthink/think-queue 原创
数据库·redis·bootstrap·thinkphp