content: package com.achobeta.redis.cache;
  
  import com.alibaba.fastjson.JSON;
  import com.alibaba.fastjson.serializer.SerializerFeature;
  import com.fasterxml.jackson.databind.JavaType;
  import com.fasterxml.jackson.databind.type.TypeFactory;
  import org.springframework.data.redis.serializer.RedisSerializer;
  import org.springframework.data.redis.serializer.SerializationException;
  
  import java.nio.charset.Charset;
  import java.nio.charset.StandardCharsets;
  import java.util.Optional;
  
  /*
   Redis使用FastJson序列化
   */
  public class FastJsonRedisSerializer<T> implements RedisSerializer<T> {
   
      public static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;
   
      private final Class<T> clazz;
   
      public FastJsonRedisSerializer(Class<T> clazz) {
          super();
          this.clazz = clazz;
      }
   
      @Override
      public byte[] serialize(T data) throws SerializationException {
          return Optional.ofNullable(data)
                  .map(t -> JSON.toJSONString(t, SerializerFeature.WriteClassName).getBytes(DEFAULT_CHARSET))
                  .orElseGet(() -> new byte[0]);
      }
   
      @Override
      public T deserialize(byte[] bytes) throws SerializationException {
          return Optional.ofNullable(bytes)
                  .filter(data -> data.length > 0)
                  .map(data -> JSON.parseObject(new String(bytes, DEFAULT_CHARSET), clazz))
                  .orElse(null);
      }
  
      protected JavaType getJavaType(Class<?> clazz) {
          return TypeFactory.defaultInstance().constructType(clazz);
      }
  }