基于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);
        
    }
相关推荐
zzb15804 小时前
RAG from Scratch-优化-query
java·数据库·人工智能·后端·spring·mybatis
炸炸鱼.6 小时前
Nginx 代理与缓存实战:正向、反向及网络层级详解
网络·nginx·缓存
kuntli7 小时前
p命名空间注入原理详解
spring
yuweiade7 小时前
【Spring】Spring MVC案例
java·spring·mvc
哈里谢顿8 小时前
Redis在jwt中的作用
redis
阿梦Anmory9 小时前
Redis配置远程访问(绑定0.0.0.0):从配置到安全实战
redis·安全·bootstrap
y = xⁿ9 小时前
【从零开始学习Redis|第七篇】Redis 进阶原理篇:消息队列、分布式锁、缓存击穿与事务实现
java·redis·学习·缓存
繁华如雪亦如歌10 小时前
Linux:Cache
缓存
阿杰真不会敲代码10 小时前
缓存一致性,缓存穿透,缓存雪崩,缓存击穿
缓存
Coder_Boy_11 小时前
分布式系统核心技术完整梳理(含分库分表、分布式事务、熔断补偿)
jvm·分布式·spring·中间件