SpringBoot Redis使用fastjson進行序列化


 在使用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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM