• spring boot+redis 的快速入门


    1.导入pom依赖

       项目依赖于spring boot parent 2.2.2.RELASE
    
    • 1
     
            
                org.springframework.boot
                spring-boot-starter-data-redis
            
            
            
                org.apache.commons
                commons-pool2
            
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    2.配置相关设置 application.yml

    spring:
      redis:
        host: localhost
        port: 6379
        database: 0
        password:
        lettuce:
          pool:
            max-active: 20
            max-wait: -1
            max-idle: 8 #空闲最大连接数
            min-idle: 0
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    3.编写config类 redis的缓存序列方案 (redis的数据是我们常见的jsong格式)

    import lombok.extern.slf4j.Slf4j;
    import org.springframework.cache.CacheManager;
    import org.springframework.cache.annotation.EnableCaching;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.redis.cache.RedisCacheConfiguration;
    import org.springframework.data.redis.cache.RedisCacheManager;
    import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
    import org.springframework.data.redis.serializer.RedisSerializationContext;
    import org.springframework.data.redis.serializer.StringRedisSerializer;
    
    import java.io.Serializable;
    import java.time.Duration;
    
    /**
     * redis配置  使用lettuce连接池
     * */
    @Configuration
    @EnableCaching//开启缓存
    @Slf4j
    public class RedisConfig {
        @Bean
        //普通redis序列化
        public RedisTemplate<String, Serializable> redisTemplate(LettuceConnectionFactory connectionFactory){
            RedisTemplate<String,Serializable> redisTemplate=new RedisTemplate<>();
            redisTemplate.setConnectionFactory(connectionFactory);
            //序列化自定义
            redisTemplate.setKeySerializer(new StringRedisSerializer());
            redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
            return redisTemplate;
        }
        //缓存序列化
        @Bean
        public CacheManager cacheManager(LettuceConnectionFactory connectionFactory){
            RedisCacheConfiguration conf=RedisCacheConfiguration.defaultCacheConfig()
                    //过期时间 600s
                    .entryTtl(Duration.ofSeconds(600))
                    //序列化自定义
                    .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))
                    .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()))
                    .disableCachingNullValues();
    
    
            RedisCacheManager cacheManager=RedisCacheManager.builder(connectionFactory)
                    .cacheDefaults(conf)
                    .build();
    
            return cacheManager;
        }
    }
    
    
    • 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
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53

    4.在代码中使用reids缓存 @Cacheable

     //前台热门老师展示 top 4
        @Cacheable(value = "index",key = "'getTopTeacher'") //redis的使用
        @Override
        public List<Teacher> getTopTeacher() {
            QueryWrapper<Teacher> teacherQueryWrapper=new QueryWrapper<>();
            teacherQueryWrapper.orderByDesc("sort");
            teacherQueryWrapper.last("limit 4");
            return teacherMapper.selectList(teacherQueryWrapper);
        }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
  • 相关阅读:
    【开发问题系列】CSV转Excel
    CAS:1802908-00-4|Dde Biotin-PEG4-alkyne|Dde 生物素-PEG4-炔烃
    网络编程学习笔记❤️
    结合NBA主题,Niantic再寻LBS AR游戏破局之道
    软考高级软件架构师学习笔记二(软件工程)
    全球商务办公解决方案巨头:CRM强助攻, 覆盖1000+伙伴的售后服务网络
    软件开发过程中的办公文档使用
    传统游戏难产 育碧瞄向Web3
    微信小程序预约视频号直播
    使用Python读取Excel文件:轻松掌握数据操作的秘诀
  • 原文地址:https://blog.csdn.net/qq_50598935/article/details/126323469