SpringBoot Redis使用fastjson进行序列化

  • Post author:
  • Post category:其他


在使用spring-data-redis,默认情况下是使用org.springframework.data.redis.serializer.JdkSerializationRedisSerializer这个类来做序列化

我们使用jackson方式:

Jackson redis序列化是spring中自带的

    @Bean(name="redisTemplate")
    public RedisTemplate<String, Object> redisTemplate() {
        Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
        template.setConnectionFactory(factory);
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(jackson2JsonRedisSerializer);
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.setDefaultSerializer(new StringRedisSerializer());
        template.afterPropertiesSet();
        return template;
    }

使用fastjson方式:

public class FastJson2JsonRedisSerializer<T> implements RedisSerializer<T> {

    public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");

    private Class<T> clazz;

    public FastJson2JsonRedisSerializer(Class<T> clazz) {
        super();
        this.clazz = clazz;
    }

    public byte[] serialize(T t) throws SerializationException {
        if (t == null) {
            return new byte[0];
        }
        return JSON.toJSONString(t, SerializerFeature.WriteClassName).getBytes(DEFAULT_CHARSET);
    }

    public T deserialize(byte[] bytes) throws SerializationException {
        if (bytes == null || bytes.length <= 0) {
            return null;
        }
        String str = new String(bytes, DEFAULT_CHARSET);

        return (T) JSON.parseObject(str, clazz);
    }

}

注册:

@Configuration
public class RedisConfig {
    @Autowired
    private RedisConnectionFactory factory;

    @Autowired
    private RedisSerializer fastJson2JsonRedisSerializer;

    //fastjson
    @Bean(name="redisTemplate")
    public RedisTemplate<String, Object> fastJsonRedisTemplate() {
        RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
        template.setConnectionFactory(factory);
        //redis开启事务
        template.setEnableTransactionSupport(true);
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(fastJson2JsonRedisSerializer);
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(fastJson2JsonRedisSerializer);
        template.setDefaultSerializer(new StringRedisSerializer());
        template.afterPropertiesSet();
        return template;
    }
}

在redis工具类中调用RedisTemplate:

@Component
public class RedisCacheUtil {

    @Autowired
    @Qualifier("redisTemplate")
    private  RedisTemplate<String, String> redisTemplate;

}


对比:

jackson方式序列化存储redis中数据:

 [
  "com.qhong.test.dependBean.Person",
  {
    "age": 20,
    "name": "name0",
    "iss": true
  }
]

[
  "java.util.ArrayList",
  [
    [
      "com.qhong.test.dependBean.Person",
      {
        "age": 20,
        "name": "name0",
        "iss": true
      }
    ],
    [
      "com.qhong.test.dependBean.Person",
      {
        "age": 21,
        "name": "name1",
        "iss": true
      }
    ],
    [
      "com.qhong.test.dependBean.Person",
      {
        "age": 22,
        "name": "name2",
        "iss": true
      }
    ]
  ]
]

上面的完全不符合json格式规范

fastjson方式序列化:

{
  "@type": "com.qhong.test.dependBean.Person",
  "age": 20,
  "iss": true,
  "name": "name0"
}

[
  {
    "@type": "com.qhong.test.dependBean.Person",
    "age": 20,
    "iss": true,
    "name": "name0"
  },
  {
    "@type": "com.qhong.test.dependBean.Person",
    "age": 21,
    "iss": true,
    "name": "name1"
  },
  {
    "@type": "com.qhong.test.dependBean.Person",
    "age": 22,
    "iss": true,
    "name": "name2"
  }
]

虽然也不是很好,但是比jackson的好多了


https://www.jianshu.com/p/138f3713618a


https://github.com/haha174/seckill