redis虽然提供了对list set hash等数据类型的支持,但是没有提供对POJO对象的支持,底层都是把对象序列化后再以字符串的方式存储的。因此,Spring data提供了若干个Serializer,主要包括:
- JacksonJsonRedisSerializer
- JdkSerializationRedisSerializer
- OxmSerializer
本文主要对比了对象的JacksonJsonRedisSerializer与对象的JdkSerializationSerializer、字符串的StringRedisSerializer与字符串的JdkSerializationSerializer
一、测试代码如下
-
package com.dream.yuxiaor.utils;
-
-
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
-
import org.springframework.data.redis.core.RedisTemplate;
-
import org.springframework.data.redis.serializer.JacksonJsonRedisSerializer;
-
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
-
import org.springframework.data.redis.serializer.StringRedisSerializer;
-
import redis.clients.jedis.JedisPoolConfig;
-
-
import java.io.Serializable;
-
-
/**
-
* Created by dev on 17-10-14.
-
*/
-
public class RedisTest {
-
private static RedisTemplate redisTemplate;
-
static {
-
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
-
jedisPoolConfig.setMaxIdle(300);
-
jedisPoolConfig.setMaxTotal(600);
-
jedisPoolConfig.setMaxWaitMillis(1000);
-
jedisPoolConfig.setTestOnBorrow(true);
-
-
JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
-
jedisConnectionFactory.setPoolConfig(jedisPoolConfig);
-
jedisConnectionFactory.setHostName(“192.168.3.36”);
-
jedisConnectionFactory.setPassword(null);
-
jedisConnectionFactory.setPort(6379);
-
jedisConnectionFactory.setTimeout(2000);
-
jedisConnectionFactory.setDatabase(0);
-
jedisConnectionFactory.afterPropertiesSet();
-
redisTemplate = new RedisTemplate();
-
redisTemplate.setConnectionFactory(jedisConnectionFactory);
-
StringRedisSerializer stringSerializer = new StringRedisSerializer();
-
redisTemplate.setKeySerializer(stringSerializer);
-
redisTemplate.setHashKeySerializer(stringSerializer);
-
}
-
-
public static void main(String[] args) {
-
writeObjectJdkSerializationRedisSerializer();
-
writeObjectJacksonJsonRedisSerializer();
-
writeStringJdkSerializationRedisSerializer();
-
writeStringStringRedisSerializer();
-
}
-
-
public static void writeObjectJdkSerializationRedisSerializer(){
-
JdkSerializationRedisSerializer jdkSerializationRedisSerializer = new JdkSerializationRedisSerializer();
-
-
redisTemplate.setValueSerializer(jdkSerializationRedisSerializer);
-
redisTemplate.setHashValueSerializer(jdkSerializationRedisSerializer);
-
redisTemplate.afterPropertiesSet();
-
-
String key = “test1:”;
-
long start = System.currentTimeMillis();
-
UserObject userObject;
-
for(int i=0;i<100000;i++){
-
userObject = new UserObject();
-
userObject.setAge(i);
-
userObject.setUserName(“name”+i);
-
redisTemplate.opsForValue().set(key+i,userObject);
-
redisTemplate.opsForValue().get(key+i);
-
}
-
System.out.println(“100000次读写 writeObjectJdkSerializationRedisSerializer test times(毫秒):”+(System.currentTimeMillis()-start));
-
}
-
-
public static void writeObjectJacksonJsonRedisSerializer(){
-
-
JacksonJsonRedisSerializer jacksonJsonRedisSerializer = new JacksonJsonRedisSerializer(UserObject.class);
-
-
redisTemplate.setValueSerializer(jacksonJsonRedisSerializer);
-
redisTemplate.setHashValueSerializer(jacksonJsonRedisSerializer);
-
redisTemplate.afterPropertiesSet();
-
-
-
String key = “test2:”;
-
long start = System.currentTimeMillis();
-
UserObject userObject;
-
for(int i=0;i<100000;i++){
-
userObject = new UserObject();
-
userObject.setAge(i);
-
userObject.setUserName(“name”+i);
-
redisTemplate.opsForValue().set(key+i,userObject);
-
redisTemplate.opsForValue().get(key+i);
-
}
-
System.out.println(“100000次读写 writeObjectJacksonJsonRedisSerializer test times(毫秒):”+(System.currentTimeMillis()-start));
-
}
-
-
public static void writeStringJdkSerializationRedisSerializer(){
-
JdkSerializationRedisSerializer jdkSerializationRedisSerializer = new JdkSerializationRedisSerializer();
-
-
redisTemplate.setValueSerializer(jdkSerializationRedisSerializer);
-
redisTemplate.setHashValueSerializer(jdkSerializationRedisSerializer);
-
redisTemplate.afterPropertiesSet();
-
-
String key = “test3:”;
-
long start = System.currentTimeMillis();
-
for(int i=0;i<100000;i++){
-
redisTemplate.opsForValue().set(key+i,“{\”userName\”:\”name”+i+“\”,\”age\”:”+i+“}”);
-
redisTemplate.opsForValue().get(key+i);
-
}
-
System.out.println(“100000次读写 writeStringJdkSerializationRedisSerializer test times(毫秒):”+(System.currentTimeMillis()-start));
-
}
-
-
public static void writeStringStringRedisSerializer(){
-
-
StringRedisSerializer stringSerializer = new StringRedisSerializer();
-
-
redisTemplate.setValueSerializer(stringSerializer);
-
redisTemplate.setHashValueSerializer(stringSerializer);
-
redisTemplate.afterPropertiesSet();
-
-
-
String key = “test4:”;
-
long start = System.currentTimeMillis();
-
for(int i=0;i<100000;i++){
-
redisTemplate.opsForValue().set(key+i,“{\”userName\”:\”name”+i+“\”,\”age\”:”+i+“}”);
-
redisTemplate.opsForValue().get(key+i);
-
}
-
System.out.println(“100000次读写 writeStringStringRedisSerializer test times(毫秒):”+(System.currentTimeMillis()-start));
-
}
-
-
public static class UserObject implements Serializable {
-
private String userName;
-
-
private int age;
-
-
public String getUserName() {
-
return userName;
-
}
-
-
public void setUserName(String userName) {
-
this.userName = userName;
-
}
-
-
public int getAge() {
-
return age;
-
}
-
-
public void setAge(int age) {
-
this.age = age;
-
}
-
}
-
}
二、执行结果比较
1、存储长度比较
-
127.0.0.1:6379> strlen test1:1
-
(integer) 115
-
127.0.0.1:6379> get test1:1
-
“\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”
-
-
127.0.0.1:6379> strlen test2:1
-
(integer) 28
-
127.0.0.1:6379> get test2:1
-
“{\”userName\“:\”name1\“,\”age\“:1}”
-
-
127.0.0.1:6379> strlen test3:1
-
(integer) 35
-
127.0.0.1:6379> get test3:1
-
“\xac\xed\x00\x05t\x00\x1c{\”userName\“:\”name1\“,\”age\“:1}”
-
-
127.0.0.1:6379> strlen test4:1
-
(integer) 28
-
127.0.0.1:6379> get test4:1
-
“{\”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