content: package jnpf.configure;
  
  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;
  import org.springframework.data.redis.serializer.StringRedisSerializer;
  
  /**
   *
   * @author JNPF开发平台组
   * @version V3.1.0
   * @copyright 引迈信息技术有限公司
   * @date 2021/3/16 10:51
   */
  @Configuration
  public class RedisConfig {
      /**
       * 注入 RedisConnectionFactory
       */
      @Autowired
      private RedisConnectionFactory factory;
  
      /**
       * 实例化 RedisTemplate 对象
       *
       * @return
       */
      @Bean
      public RedisTemplate<String, Object> redisTemplate() {
          RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
          redisTemplate.setConnectionFactory(factory);
          StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
          redisTemplate.setKeySerializer(stringRedisSerializer);
          redisTemplate.setHashKeySerializer(stringRedisSerializer);
          Jackson2JsonRedisSerializer<?> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
          redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
          redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
          redisTemplate.afterPropertiesSet();
          return redisTemplate;
      }
  
      /**
       * 实例化 HashOperations 对象,可以使用 Hash 类型操作
       *
       * @param redisTemplate
       * @return
       */
      @Bean
      public HashOperations<String, String, Object> hashOperations(RedisTemplate<String, Object> redisTemplate) {
          return redisTemplate.opsForHash();
      }
  
      /**
       * 实例化 ValueOperations 对象,可以使用 String 操作
       *
       * @param redisTemplate
       * @return
       */
      @Bean
      public ValueOperations<String, Object> valueOperations(RedisTemplate<String, Object> redisTemplate) {
          return redisTemplate.opsForValue();
      }
  
      /**
       * 实例化 ListOperations 对象,可以使用 List 操作
       *
       * @param redisTemplate
       * @return
       */
      @Bean
      public ListOperations<String, Object> listOperations(RedisTemplate<String, Object> redisTemplate) {
          return redisTemplate.opsForList();
      }
  
      /**
       * 实例化 SetOperations 对象,可以使用 Set 操作
       *
       * @param redisTemplate
       * @return
       */
      @Bean
      public SetOperations<String, Object> setOperations(RedisTemplate<String, Object> redisTemplate) {
          return redisTemplate.opsForSet();
      }
  
      /**
       * 实例化 ZSetOperations 对象,可以使用 ZSet 操作
       *
       * @param redisTemplate
       * @return
       */
      @Bean
      public ZSetOperations<String, Object> zSetOperations(RedisTemplate<String, Object> redisTemplate) {
          return redisTemplate.opsForZSet();
      }
  }
  