这是我的第一个使用spring-data-redis的应用程序,我认为我很好地理解了这些概念(过去我曾经多次使用过JDBcTemplate和RDBMS-es)。这里发生了什么......
我遇到的问题是,每次执行get(key)操作(使用ValueOperations对象)时,都会打开和关闭连接,这会导致大约1/10秒的延迟( this是服务器代码,所以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)时,我不期待这种延迟我的意思看到。
提前感谢您提供任何帮助或提示。
答案 0 :(得分:1)
基于spring-data-redis-1.7.2.RELEASE
if(!isConnectionTransactional(conn, factory)) {
if (log.isDebugEnabled()) {
log.debug("Closing Redis Connection");
}
conn.close()
}
通过日志,您可以在第205行看到“关闭Redis连接”,然后通过调用关闭方法关闭连接。
我们可以发现 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”甚至使用了池