基于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);
        
    }
相关推荐
雨中飘荡的记忆11 小时前
Multi-Agent + Skills + Spring AI 构建自主决策智能体
后端·spring
用户230636271253913 小时前
SpringAIAlibaba学习使用 ---核心API、RAG、Tool Calling
spring
RealPluto15 小时前
Spring AOP 失效排查
java·spring
JavaGuide4 天前
字节二面:Redis 能做消息队列吗?怎么实现?
redis·后端
漫霂4 天前
基于redis实现登录校验
redis·后端
程序员小崔日记4 天前
一篇文章彻底搞懂 MySQL 和 Redis:原理、区别、项目用法全解析(建议收藏)
redis·mysql·项目实战
读书笔记4 天前
CentOS 7 安装 redis-6.2.6.tar.gz 详细步骤(从源码编译到启动配置)
redis
焗猪扒饭4 天前
redis stream用作消息队列极速入门
redis·后端·go