Spring Data redis序列化的比较

redis虽然提供了对list set hash等数据类型的支持,但是没有提供对POJO对象的支持,底层都是把对象序列化后再以字符串的方式存储的。因此,Spring data提供了若干个Serializer,主要包括:

  • JacksonJsonRedisSerializer
  • JdkSerializationRedisSerializer
  • OxmSerializer

本文主要对比了对象的JacksonJsonRedisSerializer与对象的JdkSerializationSerializer、字符串的StringRedisSerializer与字符串的JdkSerializationSerializer

一、测试代码如下

  1. package com.dream.yuxiaor.utils;
  2. import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
  3. import org.springframework.data.redis.core.RedisTemplate;
  4. import org.springframework.data.redis.serializer.JacksonJsonRedisSerializer;
  5. import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
  6. import org.springframework.data.redis.serializer.StringRedisSerializer;
  7. import redis.clients.jedis.JedisPoolConfig;
  8. import java.io.Serializable;
  9. /**
  10. * Created by dev on 17-10-14.
  11. */
  12. public class RedisTest {
  13. private static RedisTemplate redisTemplate;
  14. static {
  15. JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
  16. jedisPoolConfig.setMaxIdle(300);
  17. jedisPoolConfig.setMaxTotal(600);
  18. jedisPoolConfig.setMaxWaitMillis(1000);
  19. jedisPoolConfig.setTestOnBorrow(true);
  20. JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
  21. jedisConnectionFactory.setPoolConfig(jedisPoolConfig);
  22. jedisConnectionFactory.setHostName(“192.168.3.36”);
  23. jedisConnectionFactory.setPassword(null);
  24. jedisConnectionFactory.setPort(6379);
  25. jedisConnectionFactory.setTimeout(2000);
  26. jedisConnectionFactory.setDatabase(0);
  27. jedisConnectionFactory.afterPropertiesSet();
  28. redisTemplate = new RedisTemplate();
  29. redisTemplate.setConnectionFactory(jedisConnectionFactory);
  30. StringRedisSerializer stringSerializer = new StringRedisSerializer();
  31. redisTemplate.setKeySerializer(stringSerializer);
  32. redisTemplate.setHashKeySerializer(stringSerializer);
  33. }
  34. public static void main(String[] args) {
  35. writeObjectJdkSerializationRedisSerializer();
  36. writeObjectJacksonJsonRedisSerializer();
  37. writeStringJdkSerializationRedisSerializer();
  38. writeStringStringRedisSerializer();
  39. }
  40. public static void writeObjectJdkSerializationRedisSerializer(){
  41. JdkSerializationRedisSerializer jdkSerializationRedisSerializer = new JdkSerializationRedisSerializer();
  42. redisTemplate.setValueSerializer(jdkSerializationRedisSerializer);
  43. redisTemplate.setHashValueSerializer(jdkSerializationRedisSerializer);
  44. redisTemplate.afterPropertiesSet();
  45. String key = “test1:”;
  46. long start = System.currentTimeMillis();
  47. UserObject userObject;
  48. for(int i=0;i<100000;i++){
  49. userObject = new UserObject();
  50. userObject.setAge(i);
  51. userObject.setUserName(“name”+i);
  52. redisTemplate.opsForValue().set(key+i,userObject);
  53. redisTemplate.opsForValue().get(key+i);
  54. }
  55. System.out.println(“100000次读写 writeObjectJdkSerializationRedisSerializer test times(毫秒):”+(System.currentTimeMillis()-start));
  56. }
  57. public static void writeObjectJacksonJsonRedisSerializer(){
  58. JacksonJsonRedisSerializer jacksonJsonRedisSerializer = new JacksonJsonRedisSerializer(UserObject.class);
  59. redisTemplate.setValueSerializer(jacksonJsonRedisSerializer);
  60. redisTemplate.setHashValueSerializer(jacksonJsonRedisSerializer);
  61. redisTemplate.afterPropertiesSet();
  62. String key = “test2:”;
  63. long start = System.currentTimeMillis();
  64. UserObject userObject;
  65. for(int i=0;i<100000;i++){
  66. userObject = new UserObject();
  67. userObject.setAge(i);
  68. userObject.setUserName(“name”+i);
  69. redisTemplate.opsForValue().set(key+i,userObject);
  70. redisTemplate.opsForValue().get(key+i);
  71. }
  72. System.out.println(“100000次读写 writeObjectJacksonJsonRedisSerializer test times(毫秒):”+(System.currentTimeMillis()-start));
  73. }
  74. public static void writeStringJdkSerializationRedisSerializer(){
  75. JdkSerializationRedisSerializer jdkSerializationRedisSerializer = new JdkSerializationRedisSerializer();
  76. redisTemplate.setValueSerializer(jdkSerializationRedisSerializer);
  77. redisTemplate.setHashValueSerializer(jdkSerializationRedisSerializer);
  78. redisTemplate.afterPropertiesSet();
  79. String key = “test3:”;
  80. long start = System.currentTimeMillis();
  81. for(int i=0;i<100000;i++){
  82. redisTemplate.opsForValue().set(key+i,“{\”userName\”:\”name”+i+“\”,\”age\”:”+i+“}”);
  83. redisTemplate.opsForValue().get(key+i);
  84. }
  85. System.out.println(“100000次读写 writeStringJdkSerializationRedisSerializer test times(毫秒):”+(System.currentTimeMillis()-start));
  86. }
  87. public static void writeStringStringRedisSerializer(){
  88. StringRedisSerializer stringSerializer = new StringRedisSerializer();
  89. redisTemplate.setValueSerializer(stringSerializer);
  90. redisTemplate.setHashValueSerializer(stringSerializer);
  91. redisTemplate.afterPropertiesSet();
  92. String key = “test4:”;
  93. long start = System.currentTimeMillis();
  94. for(int i=0;i<100000;i++){
  95. redisTemplate.opsForValue().set(key+i,“{\”userName\”:\”name”+i+“\”,\”age\”:”+i+“}”);
  96. redisTemplate.opsForValue().get(key+i);
  97. }
  98. System.out.println(“100000次读写 writeStringStringRedisSerializer test times(毫秒):”+(System.currentTimeMillis()-start));
  99. }
  100. public static class UserObject implements Serializable {
  101. private String userName;
  102. private int age;
  103. public String getUserName() {
  104. return userName;
  105. }
  106. public void setUserName(String userName) {
  107. this.userName = userName;
  108. }
  109. public int getAge() {
  110. return age;
  111. }
  112. public void setAge(int age) {
  113. this.age = age;
  114. }
  115. }
  116. }

二、执行结果比较

1、存储长度比较

  1. 127.0.0.1:6379> strlen test1:1
  2. (integer) 115
  3. 127.0.0.1:6379> get test1:1
  4. “\xac\xed\x00\x05sr\x00,com.dream.yuxiaor.utils.RedisTest$UserObject*\xf0\xb8\x84\xf3\r{\xc3\x02\x00\x02I\x00\x03ageL\x00\buserNamet\x00\x12Ljava/lang/String;xp\x00\x00\x00\x01t\x00\x05name1”
  5. 127.0.0.1:6379> strlen test2:1
  6. (integer) 28
  7. 127.0.0.1:6379> get test2:1
  8. “{\”userName\“:\”name1\“,\”age\“:1}”
  9. 127.0.0.1:6379> strlen test3:1
  10. (integer) 35
  11. 127.0.0.1:6379> get test3:1
  12. “\xac\xed\x00\x05t\x00\x1c{\”userName\“:\”name1\“,\”age\“:1}”
  13. 127.0.0.1:6379> strlen test4:1
  14. (integer) 28
  15. 127.0.0.1:6379> get test4:1
  16. “{\”userName\“:\”name1\“,\”age\“:1}”

2、执行时间比较,跑了三次

第一次:

100000次读写 writeObjectJdkSerializationRedisSerializer test times(毫秒):40441

100000次读写 writeObjectJacksonJsonRedisSerializer test times(毫秒):35915

100000次读写 writeStringJdkSerializationRedisSerializer test times(毫秒):35576

100000次读写 writeStringStringRedisSerializer test times(毫秒):35715

第二次:

100000次读写 writeObjectJdkSerializationRedisSerializer test times(毫秒):39137
100000次读写 writeObjectJacksonJsonRedisSerializer test times(毫秒):34792
100000次读写 writeStringJdkSerializationRedisSerializer test times(毫秒):34543
100000次读写 writeStringStringRedisSerializer test times(毫秒):33807

第三次:

100000次读写 writeObjectJdkSerializationRedisSerializer test times(毫秒):38700
100000次读写 writeObjectJacksonJsonRedisSerializer test times(毫秒):34555
100000次读写 writeStringJdkSerializationRedisSerializer test times(毫秒):34583
100000次读写 writeStringStringRedisSerializer test times(毫秒):34739

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享