如何自定义RedisTemplate以及存放一个集合列表

1. 导入RedisTemplate所需要的依赖包

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

2. 配置Redis的地址以及端口

  spring:
     host: 127.0.0.1
      port: 6379

3. 编写RedisTemplate的自定义配置类

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
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.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 1. @author yuanyuan
 */
@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(factory);

        // json序列化配置
        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);
        // String的序列化
        RedisSerializer stringSerializer = new StringRedisSerializer();

        // key采用String序列化
        redisTemplate.setKeySerializer(stringSerializer);

        // value采用Jackson序列化
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);

        // hash的key采用String序列化
        redisTemplate.setHashKeySerializer(stringSerializer);

        // hash的value采用Jackson序列化
        redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }
}

4.使用自定义的RedisTemplate,同时将查询的集合列表存放在redis中

private final static String EMPLOYEE_KEY = "employee";

    @Autowired
    @Qualifier("redisTemplate")
    private RedisTemplate redisTemplate;

    @Autowired
    private EmployeeService employeeService;

    @GetMapping("/getEmployee")
    public DataVO<Employee> getEmployee() {
        DataVO<Employee> dataVO = new DataVO<>();
        dataVO.setCode(0);
        dataVO.setMsg("");
        List<Employee> employee = employeeService.getEmployee();
        BoundListOperations boundListOps = redisTemplate.boundListOps(EMPLOYEE_KEY);
        // 判断key是否存在。如果不存在则将数据写入redis,若存在直接从redis中读取
        if (!redisTemplate.hasKey(EMPLOYEE_KEY)) {
            // 将整个列表存入redis中
            boundListOps.leftPushAll(employee);
        }
        List<Employee> employeeList = boundListOps.range(0, -1);
        dataVO.setData(employeeList);
        dataVO.setCount(employeeService.getEmployeeCount());
        return dataVO;
    }

5.通过Redis可视化工具查询数据如下
在这里插入图片描述redis中的数据是以json的形式存入
6.总结
平时工作中使用MongoDB存放json数据,没有使用过redis存储过json,只是用redis存放一些简单的键值对类型的数据。自己也是平时下班没事喜欢瞎琢磨,对编程还是比较感兴趣的。希望我的分享能给大家带来一些有用的东西。
感谢大家在百忙之中能够花时间阅读我的文章,如对redis理解有误的地方。希望大家能指出,互相交流学习,谢谢!!!!

©️2020 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页