基于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);
        
    }
相关推荐
没有bug.的程序员39 分钟前
Spring Cloud Gateway 性能优化与限流设计
java·spring boot·spring·nacos·性能优化·gateway·springcloud
linuxxx1102 小时前
django测试缓存命令的解读
python·缓存·django
oneslide4 小时前
Kubernetes环境部署Redis集群
redis·容器·kubernetes
song8546011344 小时前
线程本地(ThreadLocal)的缓存容器
缓存
是垚不是土6 小时前
构建高可用Redis:哨兵模式深度解析与Nacos微服务适配实践
redis·微服务·bootstrap
找不到、了6 小时前
缓存命中率从 50% 到 95%:缓存优化实战指南
缓存
小坏讲微服务6 小时前
SpringCloud零基础学全栈,实战企业级项目完整使用
后端·spring·spring cloud
_Jimmy_8 小时前
Nacos的三层缓存是什么
java·缓存
专注VB编程开发20年8 小时前
C#VB.NET中实现可靠的文件监控(新建、删除、改名、内容修改等事件的准确捕获)
spring·c#·.net·文件监控
原来是好奇心10 小时前
Spring Boot缓存实战:@Cacheable注解详解与性能优化
java·spring·mybatis·springboot