这是我使用
spring-data-redis的第一个应用程序,我认为我的概念非常好(过去我曾经多次使用过JDBcTemplate和RDBMS-es).这是发生了什么……
我遇到的问题是,每次我执行get(key)操作(使用ValueOperations对象)时,都会打开和关闭一个连接,这会导致大约1/10秒的延迟(这是服务器代码,所以1/10第二是实质性的).这是spring XML配置:
<!-- Redis DAO stuff -->
<bean
id="jedisPoolConfig"
class="redis.clients.jedis.JedisPoolConfig"
p:testOnBorrow="true"
p:testOnReturn="true"
p:timeBetweenEvictionRunsMillis="60000"
p:minIdle="2"
p:maxTotal="30"
p:maxIdle="10"
/>
<bean id="jedisConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:host-name="${redis.url}"
p:port="${redis.port}"
p:database="0"
p:use-pool="true"
p:pool-config-ref="jedisPoolConfig"
/>
<bean id="stringRedisSerializer"
class="org.springframework.data.redis.serializer.StringRedisSerializer"
/>
<bean id="redisTemplate"
class="org.springframework.data.redis.core.RedisTemplate"
p:connection-factory-ref="jedisConnectionFactory"
p:defaultSerializer-ref="stringRedisSerializer"
/>
这是相关的Java代码:
@Autowired
private RedisTemplate<String, String> template;
private ValueOperations<String, String> valOps;
@PostConstruct
public void init() {
logger.debug("111111111111111111111111aaaaaaaaaaaaaaaaaaaaaaa");
valOps = template.opsForValue();
}
public String getTestVal() {
logger.debug("getTestVal() function called++++++++++++++++++++");
Object testVal2 = valOps.get("akey");
testVal2 = valOps.get("akey");
testVal2 = valOps.get("akey");
testVal2 = valOps.get("akey");
testVal2 = valOps.get("akey");
testVal2 = valOps.get("akey");
logger.debug("TestVal2 returned from REdis: " + testVal2);
return null;
}
因此,相同密钥的值被检索六次.我看到的日志输出如下:
13:46:37.011 [http-nio-8080-exec-1] DEBUG com.arrow.pricing.dao.RedisDAO - getTestVal() function called++++++++++++++++++++
13:46:37.014 [http-nio-8080-exec-1] DEBUG o.s.d.r.core.RedisConnectionUtils - Opening RedisConnection
13:46:37.344 [http-nio-8080-exec-1] DEBUG o.s.d.r.core.RedisConnectionUtils - Closing Redis Connection
13:46:37.416 [http-nio-8080-exec-1] DEBUG o.s.d.r.core.RedisConnectionUtils - Opening RedisConnection
13:46:37.543 [http-nio-8080-exec-1] DEBUG o.s.d.r.core.RedisConnectionUtils - Closing Redis Connection
13:46:37.616 [http-nio-8080-exec-1] DEBUG o.s.d.r.core.RedisConnectionUtils - Opening RedisConnection
13:46:37.742 [http-nio-8080-exec-1] DEBUG o.s.d.r.core.RedisConnectionUtils - Closing Redis Connection
13:46:37.812 [http-nio-8080-exec-1] DEBUG o.s.d.r.core.RedisConnectionUtils - Opening RedisConnection
13:46:37.940 [http-nio-8080-exec-1] DEBUG o.s.d.r.core.RedisConnectionUtils - Closing Redis Connection
13:46:38.003 [http-nio-8080-exec-1] DEBUG o.s.d.r.core.RedisConnectionUtils - Opening RedisConnection
13:46:38.128 [http-nio-8080-exec-1] DEBUG o.s.d.r.core.RedisConnectionUtils - Closing Redis Connection
13:46:38.201 [http-nio-8080-exec-1] DEBUG o.s.d.r.core.RedisConnectionUtils - Opening RedisConnection
13:46:38.337 [http-nio-8080-exec-1] DEBUG o.s.d.r.core.RedisConnectionUtils - Closing Redis Connection
13:46:38.414 [http-nio-8080-exec-1] DEBUG com.arrow.pricing.dao.RedisDAO - TestVal2 returned from REdis: yo mama
我以为我已经关注了如何设置连接池的文档,但在处理像Redis这样的面向性能的平台时,我不会指望我看到这种延迟.
提前感谢您提供任何帮助或提示.
最佳答案 基于spring-data-redis-1.7.2.RELEASE
if(!isConnectionTransactional(conn, factory)) {
if (log.isDebugEnabled()) {
log.debug("Closing Redis Connection");
}
conn.close()
}
通过日志,您可以在第205行中看到“关闭Redis连接”,然后通过call close方法关闭连接.
我们可以发现conn是实现RedisConnection.在这种情况下,我们使用的实际类是JedisConnection.
见代码从第256行开始.
public void close() throws DataAccessExeception {
super.close();
// return the connection to the pool
if (pool != null) {
...balabala
}
}
连接回到池中.
现在我们知道RedisConnectionUtils alaways显示日志“Closing Redis Connection”甚至使用了池