在项目中使用RedisTemplate,spring的配置文件如下
<bean id="poolCfg" class="redis.clients.jedis.JedisPoolConfig" >
<property name="maxIdle" value="50"/>
</bean>
<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" >
<property name="poolConfig" ref="poolCfg"/>
<property name="hostName" value="192.168.56.220"/>
</bean>
<bean id="jdkSerializationRedisSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" >
</bean>
<bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer" >
</bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" >
<property name="keySerializer" ref="stringRedisSerializer"/>
<property name="valueSerializer" ref="jdkSerializationRedisSerializer"/>
<property name="connectionFactory" ref="connectionFactory"/>
</bean>
测试往redis写入1000个对像
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
RedisTemplate rs=ctx.getBean(RedisTemplate.class);
Long start=System.currentTimeMillis();
for(int i=0;i<1000;i++) {
Role role=new Role(String.valueOf(i)+"test");
rs.opsForValue().set("test"+i, role);
}
System.out.println("total:"+(System.currentTimeMillis()-start));
}
使用RedisTemplate批量写入数据

本文介绍如何利用Spring框架中的RedisTemplate进行批量数据写入操作,并展示了具体的配置及实现代码。
755

被折叠的 条评论
为什么被折叠?



