基于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);
        
    }
相关推荐
Wy_编程27 分钟前
Redis数据类型和常用命令
数据库·redis·缓存
朝新_2 小时前
【Spring AI 】核心知识体系梳理:从入门到实战
java·人工智能·spring
ZC跨境爬虫2 小时前
通俗易懂讲解分布式爬虫基础概念(附Scrapy-Redis实操教程)
redis·分布式·爬虫·python·scrapy
谁怕平生太急3 小时前
面试题记录:在线数据迁移
java·数据库·spring
aXin_ya3 小时前
Redis 原理篇 (数据结构)
数据库·redis·缓存
身如柳絮随风扬3 小时前
Redis 单线程模型
redis
云烟成雨TD5 小时前
Spring AI Alibaba 1.x 系列【18】Hook 接口和四大抽象类
java·人工智能·spring
不会写DN5 小时前
Go 项目中 Redis 缓存的实用设计与实现(Cache-Aside 模式)
redis·缓存·golang
aXin_ya6 小时前
Redis 网络模型 内存回收
数据库·redis·缓存
.柒宇.6 小时前
Redis
数据库·redis·缓存