基于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);
        
    }
相关推荐
L-影8 小时前
HashMap详解-上篇
java·开发语言·spring·hashmap
她说..9 小时前
幂等问题分析
java·jvm·spring boot·spring·spring cloud·intellij-idea
hweiyu0013 小时前
Redis命令:XRANGE
redis·缓存
我不会起名字32215 小时前
多级缓存架构:本地缓存与 Redis 一致性的 4 个边界
redis·高并发·多级缓存·本地缓存·后端架构
好家伙VCC15 小时前
1T4R RFSoC 100GE 回传、DDR 缓存、带宽瓶颈分析
缓存
二宝哥16 小时前
07.Spring AI 接入智谱文生图:从配置到实战
java·人工智能·spring
向哆哆17 小时前
在鸿蒙PC上用ArkUI开发跨平台缓存可视化应用:CacheLoom实战
缓存·华为·harmonyos
二宝哥19 小时前
08.Spring AI 结合 Ollama:本地大模型应用开发实战
java·人工智能·spring
鱼很腾apoc20 小时前
【Redis】第4期 详解主从复制、哨兵、集群
linux·redis·学习·集群·主从复制·哨兵