springboot 整合redis

springboot2.0 与redis整合默认使用的是Lettuce,相较于jedis,lettuce 是一个可伸缩的,线程安全的redis客户端。在处理高并发方面有更多的优势。

RedisTemplate 使用

  1. 依赖, 主要需要的依赖为

     compile('org.springframework.boot:spring-boot-starter-data-redis')
     compile('org.apache.commons:commons-pool2:2.6.0')
     compile('com.alibaba:fastjson:1.2.33')
    
  2. yml 配置

     spring:
       redis:
         database: 1
         host: 192.168.1.XX
         port: 6379
         lettuce:
           pool:
             max-active: 8
             max-wait: -1ms
             min-idle: 0
    
  3. redisTemplate 配置

    
    @Configuration
    public class RedisConfiguration {
    
    @Bean
    public RedisTemplate<String,Object> redisTemplate(LettuceConnectionFactory connectionFactory){
        RedisTemplate template = new RedisTemplate();
        template.setConnectionFactory(connectionFactory);
    
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
    
        template.setValueSerializer(jackson2JsonRedisSerializer);
        template.setKeySerializer(new StringRedisSerializer());
        template.afterPropertiesSet();
    
    
        return  template;
        }
    }
    
  4. 使用

     @Autowired
    RedisTemplate redisTemplate;
     public void addData(){
        MyInfo myInfo = new MyInfo("11","22","s344");
        redisTemplate.opsForValue().set("11",myInfo);
        redisTemplate.opsForValue().set("myse","mys");
    }
    

spring-cache 与 redis 整合

spring cache 可以在不更改现有代码逻辑的基础上,通过增加注解的方式,实现缓存。 减少代码侵入

  1. 在gradle文件中增加 spring-cache的相关依赖 compile('org.springframework.boot:spring-boot-starter-cache')

  2. 配置CacheManager, 在Configuration 配置类上增加EnableCache 注解;

  3. 配置CacheManager Bean,代码为

        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
         ObjectMapper objectMapper = new ObjectMapper();
         objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
         objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
         jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
    
         RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory);
    
         RedisCacheConfiguration cacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();
         cacheConfiguration = cacheConfiguration.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer));
         cacheConfiguration.entryTtl(Duration.ofMinutes(10));
         CacheManager cacheManager = new RedisCacheManager(redisCacheWriter, cacheConfiguration);
         return cacheManager;
    
    

完成上述配置后,便可以在service中直接使用spring-cache注解,完整代码地址: 代码

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 47,040评论 6 342
  • springboot提供了spring-data-redis的框架来整合redis的操作。下面主要介绍,sprin...
    阿卧阅读 14,840评论 0 15
  • 整合步骤 由于springboot开箱即用的特点,所以可以预知,springboot整合redis将会非常简单,如...
    LuoHaiPeng阅读 4,597评论 0 2
  • 前言: redis是一种nosql数据库,以<key,value>的形式存储数据,其速度相比于MySQL之类的数据...
    贪挽懒月阅读 20,998评论 4 25
  • 前言 消息队列作为一种常用的异步通信解决方案,而redis是一款高性能的nosql产品,今天就给大家介绍一下,如何...
    xiaodongod阅读 23,018评论 7 10