基于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);
        
    }
相关推荐
czlczl2002092514 小时前
Redis集群批处理下的陷阱
数据库·redis·缓存
小王不爱笑13215 小时前
Mybatis面试题目
spring·面试·mybatis
人道领域15 小时前
Day | 07 【苍穹外卖:菜品套餐的缓存】
java·开发语言·redis·缓存击穿·springcache
m0_7066532315 小时前
数据库与缓存操作策略:数据一致性与并发问题
java·数据库·缓存
独断万古他化15 小时前
【抽奖系统开发实战】Spring Boot 活动模块设计:事务保障、缓存优化与列表展示
java·spring boot·redis·后端·缓存·mvc
y = xⁿ15 小时前
【黑马点评二刷日记】分布式锁和Redisson
java·redis·分布式·缓存
zdl68615 小时前
spring Profile
java·数据库·spring
prince0515 小时前
SpringBoot + 多级缓存(Caffeine + Redis + 空值缓存):防穿透、防雪崩、低延迟三合一
spring boot·redis·缓存
Fang fan15 小时前
高并发、分布式场景下的ID生成策略
数据库·redis·分布式·缓存
武超杰16 小时前
SpringMVC入门指南:从零开始掌握核心用法
java·spring·mvc