基于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);
        
    }
相关推荐
于眠牧北9 分钟前
分布式环境在@Transation注解下锁释放问题
spring boot·redis·分布式
拾贰_C12 分钟前
【centos7 | Linux | redis】Redis安装
linux·运维·redis
MX_935914 分钟前
Spring的xml方式声明式事务控制
xml·java·后端·spring
fengxin_rou20 分钟前
redis主从和集群一致性、哨兵机制详解
java·开发语言·数据库·redis·缓存
Olafur_zbj20 分钟前
【AI】LLM上下文拼接
java·开发语言·spring·llm·context
不光头强21 分钟前
integer修改缓存大小
缓存
对酒当歌丶人生几何23 分钟前
Spring异步体系与事务一致性实战指南
java·spring·eventlistener
那我掉的头发算什么29 分钟前
【Linux】Linux基本使用和程序部署
linux·运维·服务器·数据库·spring·mybatis
LSL666_32 分钟前
11 redis核心配置参数
数据库·redis·缓存