基于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);
        
    }
相关推荐
NiceCloud喜云3 小时前
Opus 4.8 的 Effort Control 怎么选:Low 到 Max 五档策略
android·java·大数据·前端·c++·python·spring
程序员老邢4 小时前
《技术底稿 43》今日踩坑复盘:Redis 乱码 + MySQL 配置注入失败
redis·技术底稿·redisson 序列化·mysql 配置·项目踩坑·微服务问题排查
云烟成雨TD6 小时前
Spring AI Alibaba 1.x 系列【63】AI Agent 长期记忆
java·人工智能·spring
Mr. zhihao7 小时前
Redis五大高级数据结构:原理-场景-底层-横向对比
数据结构·redis
C137的本贾尼7 小时前
Spring AI Alibaba 模型全家桶:接入通义、百川、LLaMA 等第三方 LLM
人工智能·spring·llama
C+-C资深大佬7 小时前
SSM 框架(Spring + SpringMVC + MyBatis)
java·spring·mybatis
云烟成雨TD8 小时前
Spring AI Alibaba 1.x 系列【64】 ReactAgent 长期记忆
java·人工智能·spring
JacksonMx8 小时前
@Transactional 最佳实践
java·spring boot·spring·性能优化
YL2004042611 小时前
【Redis实战篇】秒杀实现方案(以优惠券秒杀为例)
数据库·redis
better_liang12 小时前
每日Java面试场景题知识点之-如何设计分布式锁
java·redis·zookeeper·面试·分布式锁