这是我第一个使用 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 连接”,然后通过调用 close 方法关闭连接。
我们可以发现conn是RedisConnection的实现。在这种情况下,我们实际使用的类是JedisConnection。
请参阅代码从第 256 行开始。
public void close() throws DataAccessExeception {
super.close();
// return the connection to the pool
if (pool != null) {
...balabala
}
}
连接回到池中。 现在我们知道 RedisConnectionUtils 总是显示日志“Closing Redis Connection”甚至使用了池
关于spring-data-redis 连接池没有像我预期的那样工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35805771/