基于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);
        
    }
相关推荐
Java水解17 小时前
微服务架构下Spring Session与Redis分布式会话实战全解析
后端·spring
momo学习版21 小时前
带你实现基于 Redis 的分布式 Session 管理
redis
雨中飘荡的记忆2 天前
Multi-Agent + Skills + Spring AI 构建自主决策智能体
后端·spring
用户23063627125392 天前
SpringAIAlibaba学习使用 ---核心API、RAG、Tool Calling
spring
RealPluto2 天前
Spring AOP 失效排查
java·spring
JavaGuide5 天前
字节二面:Redis 能做消息队列吗?怎么实现?
redis·后端
漫霂5 天前
基于redis实现登录校验
redis·后端
程序员小崔日记5 天前
一篇文章彻底搞懂 MySQL 和 Redis:原理、区别、项目用法全解析(建议收藏)
redis·mysql·项目实战
读书笔记5 天前
CentOS 7 安装 redis-6.2.6.tar.gz 详细步骤(从源码编译到启动配置)
redis