zoukankan      html  css  js  c++  java
  • springboot整合redis

    1、加入redis依赖

     <!-- redis -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-redis</artifactId>
            </dependency>
    
            <!-- spring2.X集成redis所需common-pool2-->
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-pool2</artifactId>
                <version>2.6.0</version>
            </dependency>
    

    2、编写redis配置类

    /**
     * author LiQinZhen
     * date 2020/10/13
     * description: resdis配置类
     */
    @Configuration
    @EnableCaching
    public class RedisConfig extends CachingConfigurerSupport {
        @Bean
    
        public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
    
            RedisTemplate<String, Object> template = new RedisTemplate<>();
    
            RedisSerializer<String> redisSerializer = new StringRedisSerializer();
    
            Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
    
            ObjectMapper om = new ObjectMapper();
    
            om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    
            om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
    
            jackson2JsonRedisSerializer.setObjectMapper(om);
    
            template.setConnectionFactory(factory);
    
            //key序列化方式
    
            template.setKeySerializer(redisSerializer);
    
            //value序列化
    
            template.setValueSerializer(jackson2JsonRedisSerializer);
    
            //value hashmap序列化
    
            template.setHashValueSerializer(jackson2JsonRedisSerializer);
    
            return template;
    
        }
    
    
    
        @Bean
    
        public CacheManager cacheManager(RedisConnectionFactory factory) {
    
            RedisSerializer<String> redisSerializer = new StringRedisSerializer();
    
            Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
    
            //解决查询缓存转换异常的问题
    
            ObjectMapper om = new ObjectMapper();
    
            om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    
            om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
    
            jackson2JsonRedisSerializer.setObjectMapper(om);
    
            // 配置序列化(解决乱码的问题),过期时间600秒
    
            RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
    
                    .entryTtl(Duration.ofSeconds(600))
    
                    .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer))
    
                    .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer))
    
                    .disableCachingNullValues();
    
            RedisCacheManager cacheManager = RedisCacheManager.builder(factory)
    
                    .cacheDefaults(config)
    
                    .build();
    
            return cacheManager;
    
        }
    }

    3、在相关的方法上面添加注解

    @Service
    public class CrmBannerServiceImpl extends ServiceImpl<CrmBannerMapper, CrmBanner> implements CrmBannerService {
        //查询所有Banner
        @Cacheable(key="'selectIndexList'",value="banner")
        @Override
        public List<CrmBanner> selectAllBanner() {
            QueryWrapper<CrmBanner> wrapper = new QueryWrapper<>();
            //根据id降序排序
            wrapper.orderByDesc("id");
            //用last拼接sql语句
            wrapper.last("limit 2");
            List<CrmBanner> list = baseMapper.selectList(null);
            return list;
        }
    }

    4、application.properties中加入redis相关配置

    # Redis数据库索引(默认为0) 
    spring.redis.database=0 
    # Redis服务器地址 
    spring.redis.host=127.0.0.1
    # Redis服务器连接端口 
    spring.redis.port=6379 
    # Redis服务器连接密码(默认为空) 
    spring.redis.password= 
    # 连接池最大连接数(使用负值表示没有限制) 
    spring.redis.pool.max-active=200 
    # 连接池最大阻塞等待时间(使用负值表示没有限制) 
    spring.redis.pool.max-wait=-1 
    # 连接池中的最大空闲连接 
    spring.redis.pool.max-idle=10
    # 连接池中的最小空闲连接 
    spring.redis.pool.min-idle=0 
    # 连接超时时间(毫秒) 
    spring.redis.timeout=1000
     
  • 相关阅读:
    select poll使用
    linux下tomcat shutdown后 java进程依然存在
    poj 1222 EXTENDED LIGHTS OUT(高斯消元)
    poj 2377 Bad Cowtractors
    C#:总结页面传值几种方法
    从 Racket 入门函数式编程
    程序猿接私活经验总结,来自csdn论坛语录
    什么是SEO?SEO干嘛的?怎么做SEO?
    Java设计模式-观察者模式
    非常特别的一个动态规划新手教程
  • 原文地址:https://www.cnblogs.com/liqinzhen/p/13878634.html
Copyright © 2011-2022 走看看