• springboot配置redis缓存


    网上乱七八糟的教程太多,太复杂!

    pom.xml配置

    <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-cache</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-redis</artifactId>
            </dependency>
    

    application.yml配置

    spring:
      redis:
        database: 0
        host: 192.168.1.102
        port: 6379
      cache:
        type: redis
    

    redis配置

    package com.fooww.research.housesourcelist.config;
    
    import com.fasterxml.jackson.annotation.JsonAutoDetect;
    import com.fasterxml.jackson.annotation.PropertyAccessor;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import org.springframework.cache.CacheManager;
    import org.springframework.cache.annotation.CachingConfigurerSupport;
    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.cache.RedisCacheWriter;
    import org.springframework.data.redis.connection.RedisConnectionFactory;
    import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
    import org.springframework.data.redis.serializer.RedisSerializationContext;
    
    import java.time.Duration;
    
    /**
     * @author :zwy
     */
    @Configuration
    public class RedisConfig extends CachingConfigurerSupport {
    
        @Bean
        public CacheManager cacheManager(RedisConnectionFactory factory) {
            //对象的序列化
            RedisSerializationContext.SerializationPair valueSerializationPair
                    = RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer());
            //全局redis缓存过期时间
            RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
                    .entryTtl(Duration.ofHours(1L))
    //                .serializeKeysWith()
                    .serializeValuesWith(valueSerializationPair);
    
            return new RedisCacheManager(RedisCacheWriter.nonLockingRedisCacheWriter(factory), redisCacheConfiguration);
        }
    
        private Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer() {
            Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
            jackson2JsonRedisSerializer.setObjectMapper(objectMapper());
            return jackson2JsonRedisSerializer;
        }
    
        private ObjectMapper objectMapper() {
            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
            objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
            return objectMapper;
        }
    }
    

     启动类

    @SpringBootApplication
    @EnableCaching
    public class HousesourcelistApplication {
    
      public static void main(String[] args) {
        SpringApplication.run(HousesourcelistApplication.class, args);
      }
    }
    

      

  • 相关阅读:
    如何在当前文件夹打开cmd(基于win10)
    Python命名空间和作用域
    Python定位模块_PYTHONPATH变量
    Python模块_import语句_from...import 函数名_from ... import *
    Python全局变量和局部变量
    Python变量作用域
    Python匿名函数_return语句
    Python函数参数
    Python按值传递参数和按引用传递参数
    Python函数调用
  • 原文地址:https://www.cnblogs.com/zhouwenyang/p/12841353.html
Copyright © 2020-2023  润新知