config:
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="300"/>
<property name="maxTotal" value="60000"/>
<property name="testOnBorrow" value="true"/>
</bean>
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="poolConfig" ref="jedisPoolConfig"></property>
<property name="hostName" value="${redis.host}"></property>
<property name="port" value="${redis.port}"></property>
<property name="timeout" value="${redis.timeout}"></property>
<property name="password" value="${redis.password}"></property>
<property name="database" value="${redis.database.business}"></property>
</bean>
<bean id="keySerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="keySerializer" ref="keySerializer"></property>
<property name="connectionFactory" ref="jedisConnectionFactory"></property>
</bean>
测试用例
@Benchmark
public void jedis(TestBenchmark state) {
for (int i = 0; i < 1000; i++) {
Jedis jedis = jedisPool.getResource();
jedis.hset("HA", "FA" + i, "VV" + i);
jedis.close();
}
for (int i = 0; i < 1000; i++) {
Jedis jedis = jedisPool.getResource();
jedis.hget("HA", "FA" + i);
jedis.close();
}
}
@Benchmark
public void springTemplate(TestBenchmark state) {
for (int i = 0; i < 1000; i++) {
template.opsForHash().put("HA", "FA" + i, "VV" + i);
}
for (int i = 0; i < 1000; i++) {
template.opsForHash().get("HA", "FA" + i);
}
}
结果:
# Run complete. Total time: 00:04:45
Benchmark Mode Cnt Score Error Units
TestBenchmark.jedis avgt 20 4063712.941 ± 774497.669 us/op
TestBenchmark.springTemplate avgt 20 8127876.567 ± 689718.613 us/op
是否有可能通过jedis来创建更多java对象,或者还有其他原因?