• 采用redis缓存优化项目


    一、使用redis缓存
    1、导入maven坐标

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

    2、在项目application.yml中加入redis相关配置

    spring:
    	redis:
        	host: 127.0.0.1
        	port: 6379
        	password: 123456
        	database: 0
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    3、新建redis的配置类,修改key序列化器
    redis默认的序列化器生成的数据不便于查看
    配置前:
    在这里插入图片描述
    配置后:
    在这里插入图片描述

    package com.mp.rj.config;
    
    import org.springframework.cache.annotation.CachingConfigurerSupport;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.redis.connection.RedisConnectionFactory;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.data.redis.serializer.StringRedisSerializer;
    
    @Configuration
    public class RedisConfig extends CachingConfigurerSupport {
        @Bean
        public RedisTemplate redisTemplate(RedisConnectionFactory connectionFactory) {
            RedisTemplate redisTemplate = new RedisTemplate<>();
            //默认的Key序列化器为:JdkSerializationRedisSerializer
            redisTemplate.setKeySerializer(new StringRedisSerializer());
            redisTemplate.setConnectionFactory(connectionFactory);
            return redisTemplate;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    3、业务场景下使用

    @Resource
     private RedisTemplate redisTemplate;
     
    //将生成的验证码缓存到redis中,并设置有效期为5分钟
    redisTemplate.opsForValue().set(phone,code,5, TimeUnit.MINUTES);
    
    //从redis中获取缓存的验证码
    Object codeInRedis = redisTemplate.opsForValue().get(phone);
    
    //如果用户登录成功,删除redis中缓存的验证码
    redisTemplate.delete(phone);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    4、分支下的优化测试无bug后合并回主分支
    切换回主分支:idea右下角分支->master->checkout
    在这里插入图片描述
    将分支修改合并到主分支:idea右下角分支->修改的分支->mergeintocurrent
    在这里插入图片描述
    二、使用Spring Cache简化实现缓存功能
    1、Spring Cache是一个框架,实现了基于注解的缓存功能。Spring Cache提供了一层抽象,底层可以切换不同的cache实现,具体就是通过CacheManager接口来提不同的缓存技术。CacheManager是Spring提供的各种缓存技术抽象接口。
    在这里插入图片描述
    2、使用redis缓存技术
    导入坐标

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

    3、配置application.yml

    spring:
    	redis:
        	host: 127.0.0.1
        	port: 6379
        	password:
        	database: 0
     	 cache:
        	redis:
          		time-to-live: 1800000 #设置缓存过期时间
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    2、启动类加入@EnableCaching注解开启缓存注解功能

    3、在Controller的方法上加入@Cacheable、@CacheEvict等注解进行缓存操作
    在这里插入图片描述

  • 相关阅读:
    中兴PTN常用命令解析
    Vue 中利用 template标签遍历多维数组
    SpringBoot jackson 配置localDate、localDateTime日期序列化,反序列化
    【MySQL入门指北】MySQL 彻底删除
    128.《usestate与usestate区别及应用场景》
    JS for循环语句的用法
    SpringBoot源码篇(二)启动过程
    人像分割技术解析与应用
    Android APN 参数数据库设计和代码实现
    SpringBoot自动配置原理
  • 原文地址:https://blog.csdn.net/qq_43570799/article/details/127665183