基于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);
        
    }
相关推荐
FungLeo8 小时前
成为全栈·基础补充·缓存的层次与失效:浏览器、CDN、服务端、数据库
缓存·cdn·数据一致性·http缓存·成为全栈·flutter缓存
我不会起名字32211 小时前
Redis 从 0 到 1(三十三):机器断电 Redis 起不来——AOF/RDB 损坏的 4 步抢救流程
redis·数据恢复·aof·rdb·排查
code2cat14 小时前
【随笔】KV缓存量化:省下的显存,要付出多少数值误差
人工智能·缓存·大语言模型
csdn_aspnet14 小时前
Redis 入门:Windows、Mac安装指南
数据库·windows·redis·缓存·mac
IT规划师15 小时前
Redis 数据类型全解 - HyperLogLog:12 KB 估出亿级 UV(八)
redis·uv·hyperloglog·.net10
Pniubi15 小时前
缓存击穿的解决方案
缓存
aiks_yuan1 天前
Token 放 Redis 还是用 JWT:Java 实测验签和查 Redis 耗时,退出登录、改密码踢下线怎么做(jjwt 0.12 + Redis 7.2)
redis
露天赏雪1 天前
第3篇:Sentinel熔断降级实战:大模型接口抖动时的服务兜底方案
java·开发语言·人工智能·spring boot·spring·servlet·sentinel
captain3761 天前
▲统一功能——DispatcherServlet源码分析
java·spring·java-ee·log4j·maven