Configuration file:
<!-- jdk序列化 -->
<!-- <property name="valueSerializer">
<bean
class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
</property> -->
<!-- json序列化 -->
<property name="valueSerializer">
<bean
class="org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer" >
<constructor-arg value="java.lang.Object"></constructor-arg>
</bean>
</property>
<!-- hash小键的序列化 -->
<property name="hashKeySerializer">
<bean
class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
Test class
package com.lyn;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.lyn.common.utils.RandomUtil;
import com.lyn.common.utils.StringUtil;
import com.lyn.entity.User;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring-redis.xml")
public class RedisTest {
@Resource
private RedisTemplate<String, User> redisTemplate;
@Test
public void testJDK() {
ListOperations<String, User> opsForList = redisTemplate.opsForList();
List<User> list = new ArrayList<User>();
for(int i=0;i<=50000;i++) {
User user = new User(i, StringUtil.randomChineseString(3), "13"+RandomUtil.randomNumber(9));
list.add(user);
}
long t1 = System.currentTimeMillis();
opsForList.leftPushAll("user_list1", list);
long t2 = System.currentTimeMillis();
System.out.println("所用时间:"+(t2-t1));
}
@Test
public void testHash() {
HashOperations<String, Object, Object> opsForHash = redisTemplate.opsForHash();
HashMap<String,User> map = new HashMap<String, User>();
for(int i=0;i<=50;i++) {
User user = new User(i, StringUtil.randomChineseString(3), "13"+RandomUtil.randomNumber(9));
map.put(""+i, user);
}
System.out.println(map);
long t1 = System.currentTimeMillis();
opsForHash.putAll("user_hash", map);
long t2 = System.currentTimeMillis();
System.out.println("所用时间:"+(t2-t1));
}
}
Test Results:
The JSON serialization method has the shortest use time, and the JDK method has the longest use time