4

这是我第一个使用 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 之类的面向性能的平台时,我想到会出现这种延迟。

提前感谢您的任何帮助或提示。

4

1 回答 1

1

基于 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”甚至使用了池

于 2017-09-12T09:09:19.810 回答