• 瑞吉外卖项目Day07-缓存优化


    瑞吉外卖项目Day07-缓存优化

    优化原因:用户数量多,系统访问量大,频繁访问数据库,系统性能下降,用户体验差

    .gitignore文件

    .git
    logs
    rebel.xml
    target/
    !.mvn/wrapper/maven-wrapper.jar
    log.path_IS_UNDEFINED
    .DS_Store
    offline_user.md
    
    ### STS ###
    .apt_generated
    .classpath
    .factorypath
    .project
    .settings
    .springBeans
    
    ### IntelliJ IDEA ###
    .idea
    *.iws
    *.iml
    *.ipr
    
    ### NetBeans ###
    nbproject/private/
    build/
    nbbuild/
    dist/
    nbdist/
    .nb-gradle/
    generatorConfig.xml
    
    ### nacos ###
    third-party/nacos/derby.log
    third-party/nacos/data/
    third-party/nacos/work/
    
    file/
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    将代码提交到giee仓库中

    创建v1.0分支

    环境搭建:

    导入依赖坐标

    
        org.springframework.boot
        spring-boot-starter-data-redis
    
    
    • 1
    • 2
    • 3
    • 4

    在项目的配置文件中加入redis相关配置

    在spring目录下

    redis:
      host: 127.0.0.1
      port: 6379
      database: 0
    
    • 1
    • 2
    • 3
    • 4

    加入配置类

    @Configuration
    public class RedisConfig extends CachingConfigurerSupport {
        
            @Bean
            public RedisTemplate<Object,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
                RedisTemplate<Object,Object> redisTemplate=new RedisTemplate<>();
    
                //默认的Key序列化器为:JdkSerializerRedisSerializer
                redisTemplate.setKeySerializer(new StringRedisSerializer());
                
    
                redisTemplate.setConnectionFactory(redisConnectionFactory);
                return redisTemplate;
            }
        
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    上传至gitee仓库中的v1.0分支

    缓存短信验证码

    1.在服务端UserController中注入RedisTemplate对象,用于操作Redis

    @Autowired
    private RedisTemplate redisTemplate;
    
    • 1
    • 2

    2.服务端UserController的sendMsg方法中,将随机生成的验证码缓存到Redis中,并设置有效时间5分钟

    //将生成的验证码缓存到Redis中,并设置有效期为5分钟
    redisTemplate.opsForValue().set(phone,code,5, TimeUnit.MINUTES);
    
    • 1
    • 2

    3.在服务端UserControlelr的login方法中,从Redis中获取缓存的验证码,如果登录成功则删除Redis中的验证码

     //获取Redis中的验证码
    String s=redisTemplate.opsForValue().get(phone).toString();;
    
    • 1
    • 2
    //用户登录成功删除Redis缓存的验证码
    redisTemplate.delete(phone);
    return R.success(user);
    
    • 1
    • 2
    • 3

    缓存菜品数据

    1.改造DishController中的list方法,先将Redis中获取菜品数据,如果有则直接返回,无需查询数据库,如果没有则查询数据库,并将查询到的数据缓存到Redis中

    List<DishDto> dishDtoList=null;
    //动态构造key
    String key="dish_"+dish.getCategoryId()+"_"+dish.getStatus();
    //先从Redis中获取返回数据
    dishDtoList= (List<DishDto>) redisTemplate.opsForValue().get(key);
    
    if (dishDtoList!=null){
        //如果存在直接返回
        return R.success(dishDtoList);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    //不存在,查询数据库,将查询到的菜品数据缓存到Redis
    redisTemplate.opsForValue().set(key,dishDtoList,60, TimeUnit.MINUTES);
    
    return R.success(dishDtoList);
    
    • 1
    • 2
    • 3
    • 4

    2.改造DishController中的save和update方法,加入缓存的逻辑

    在使用缓存的过程中,要注意保证数据库中的数据和缓存的一致性,如果数据库中的数据发生变化,需要及时清理缓存数据

    //清理所有菜品的缓存数据
    //Set keys = redisTemplate.keys("dish_*");
    //清理某个分类下的菜品缓存
    String key="dish_"+dishDto.getCategoryId()+"_1";
    redisTemplate.delete(key);
    
    • 1
    • 2
    • 3
    • 4
    • 5

    推送至远程仓库

    并且在本地仓库中合并分支(测试v1.0没有错误后)

    缓存套餐数据

    导入Spring Cache和Redis相关的maven坐标

    
        org.springframework.boot
        spring-boot-starter-data-redis
    
    
        org.springframework.boot
        spring-boot-starter-cache
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    在application.yml中配置缓存数据过期时间:

    cache: 
       redis: 
         time-to-live: 1800000  #设置缓存数据的过期时间
    
    • 1
    • 2
    • 3

    开启注解缓存@EnableCaching

    查询套餐数据:

    在SetmeanController中list方法中使用@Cacheable注解

    由于返回的是R对象没有实现序列化接口,所以报错DefaultSerializer requires

    将R实现序列化接口

    @Data
    public class R<T> implements Serializable ...{
    }
    
    • 1
    • 2
    • 3
       @Cacheable(value = "setmealCache",key = "#setmeal.categoryId+'_'+#setmeal.status")
        @GetMapping("/list")
        public R<List<Setmeal>> list(Setmeal setmeal){
            LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
            queryWrapper.eq(setmeal.getStatus() != null,Setmeal::getStatus,setmeal.getStatus());
            queryWrapper.orderByDesc(Setmeal::getUpdateTime);
    
            List<Setmeal> list = setmealService.list(queryWrapper);
    
            return R.success(list);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    删除套餐和新增套餐:

    allEntries = true 表示删除setmealCache分类下的所有数据

    在删除套餐和新增套餐的方法上添加注解

    @CacheEvict(value = "setmealCache",allEntries = true)
    
    • 1

    将代码提交远程仓库

  • 相关阅读:
    知识蒸馏(Knowledge Distillation)简述
    R语言顶级期刊配色
    Vue 2.0的源码构建
    零犀科技携手集智俱乐部:“因果派”论坛成功举办,“因果革命”带来下一代可信AI
    凌恩客户文献|宏基因组binning解析中国浓味白酒窖泥微生物群落MAGs和代谢潜力
    YOLO V5源码详解
    GitHub Codespaces 安装 .NET 7
    spirng boot 打包,胖fat包和瘦thin包
    微服务架构整理-(十一、SpringCloud实战之OpenFeign)
    representation learning for resource-constrained keyphrase generation
  • 原文地址:https://blog.csdn.net/qq_57907966/article/details/126281257