springboot 集成redis

保持耐心

官网
https://spring.io/projects/spring-data-redis

集成功能文档
https://docs.spring.io/spring-data/redis/docs/2.2.5.RELEASE/reference/html/#reference

api文档
https://docs.spring.io/spring-data/redis/docs/2.2.5.RELEASE/api/

springboot集成redis

添加spring-boot-starter-data-redis依赖


       
           org.springframework.boot
           spring-boot-starter-data-redis
       

编辑application-dev.yml,添加如下配置

redis:
    open: false  # 是否开启redis缓存  true开启   false关闭
    database: 0
    host: localhost
    port: 6379
    password:  123456  # 密码(默认为空)
    timeout: 6000ms  # 连接超时时长(毫秒)
    jedis:
      pool:
        max-active: 1000  # 连接池最大连接数(使用负值表示没有限制)
        max-wait: -1ms      # 连接池最大阻塞等待时间(使用负值表示没有限制)
        max-idle: 10      # 连接池中的最大空闲连接
        min-idle: 5       # 连接池中的最小空闲连接
添加redis配置类 RedisConfig如下
  • 通过RedisConnectionFactory 类构造 一个RedisTemplate和redis的5种数据类型操作bean
  • 指定序列化方式为 Jackson2JsonRedisSerializer
package com.springboot.study.demo1.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
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.*;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;


/**
 *@description: RedisConfig
 *@author: yinkai
 *@create: 2020/3/7 21:26
 */
@Configuration
public class RedisConfig {

    /**
     * 自己配置一个RedisTemplate Bean
     * @return
     */
    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory){
        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);
        RedisTemplate template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);
        template.setKeySerializer(jackson2JsonRedisSerializer);
        template.setValueSerializer(jackson2JsonRedisSerializer);
        template.setHashKeySerializer(jackson2JsonRedisSerializer);
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();

        //开启事务支持
       // template.setEnableTransactionSupport(true);
        return template;
    }


    /**
     * 自己配置一个StringRedisTemplate Bean,解决  haskey方法返回false
     * @return
     */
    @Bean
    public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory factory){
        StringRedisTemplate template = new StringRedisTemplate(factory);
        //定义key序列化方式
        //RedisSerializer redisSerializer = new StringRedisSerializer();//Long类型会出现异常信息;需要我们上面的自定义key生成策略,一般没必要
        //定义value的序列化方式
        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.setValueSerializer(jackson2JsonRedisSerializer);
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();

        //不能在StringRedisTemplate开启事务,会报空指针异常。可能是cglib代理的问题
//        template.setEnableTransactionSupport(true);
        return template;
    }

    /**
     * 配置redis中 5 种数据类型 bean
     * @param redisTemplate
     * @return
     */
    @Bean
    public HashOperations hashOperations(RedisTemplate redisTemplate) {
        return redisTemplate.opsForHash();
    }

    @Bean
    public ValueOperations valueOperations(RedisTemplate redisTemplate) {
        return redisTemplate.opsForValue();
    }

    @Bean
    public ListOperations listOperations(RedisTemplate redisTemplate) {
        return redisTemplate.opsForList();
    }

    @Bean
    public SetOperations setOperations(RedisTemplate redisTemplate) {
        return redisTemplate.opsForSet();
    }

    @Bean
    public ZSetOperations zSetOperations(RedisTemplate redisTemplate) {
        return redisTemplate.opsForZSet();
    }



}


 
 
具体对Redis功能的调用

使用以下 7个类就能针对redis进行数据操作
RedisTemplate 里面有一些公用方法,StringRedisTemplate 是RedisTemplate 的子类,请使用StringRedisTemplate来进行操作,不然可能出现一些奇怪的序列化问题。其它5个对应redis的5种数据类型

   @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private ValueOperations valueOperations;
    @Autowired
    private HashOperations hashOperations;
    @Autowired
    private ListOperations listOperations;
    @Autowired
    private SetOperations setOperations;
    @Autowired
    private ZSetOperations zSetOperations;

下面对redis的5中数据类型一一操作
https://www.jianshu.com/p/f65b78e68ea4

你可能感兴趣的:(springboot 集成redis)