Redis - Redis频繁打开关闭连接(RedisConnectionUtils),有性能问题???

    今天在测试代码的时候无意中发现,使用springboot-redis连接的Redis,在读写数据的时候,日志中总是打印“Opening RedisConnection” “Closing Redis Connection”;

13:22:46.343 [LService-16] DEBUG o.s.d.r.core.RedisConnectionUtils - Opening RedisConnection
13:22:46.346 [LService-16] DEBUG o.s.d.r.core.RedisConnectionUtils - Closing Redis Connection
13:22:46.347 [LService-16] DEBUG o.s.d.r.core.RedisConnectionUtils - Opening RedisConnection
13:22:46.349 [LService-16] DEBUG o.s.d.r.core.RedisConnectionUtils - Closing Redis Connection
13:22:46.349 [LService-16] DEBUG o.s.d.r.core.RedisConnectionUtils - Opening RedisConnection
13:22:46.351 [LService-16] DEBUG o.s.d.r.core.RedisConnectionUtils - Closing Redis Connection
13:22:46.351 [LService-16] DEBUG o.s.d.r.core.RedisConnectionUtils - Opening RedisConnection
13:22:46.353 [LService-16] DEBUG o.s.d.r.core.RedisConnectionUtils - Closing Redis Connection

    是不是每一次的读写都在创建和销毁连接?那岂不是很耗费资源?是不是效率很低下???

    其实,并不是这样的。阅读源代码我们可以发现我们对redis的所有操作都是通过回调execute函数执行的,spring-boot-redis内部为我们封装管理了连接池;性能那块也不用担心。

public <T> T execute(RedisCallback<T> action, boolean exposeConnection) {  
    return execute(action, exposeConnection, false);  
}  
// execute实现如下:  
// org.springframework.data.redis.core.RedisTemplate<K, V> --- 最终实现  
public <T> T execute(RedisCallback<T> action, boolean exposeConnection, boolean pipeline) {  
    Assert.isTrue(initialized, "template not initialized; call afterPropertiesSet() before using it");  
    Assert.notNull(action, "Callback object must not be null");  
    RedisConnectionFactory factory = getConnectionFactory();  
    RedisConnection conn = null;  
    try {  
        if (enableTransactionSupport) {  
            // only bind resources in case of potential transaction synchronization  
            conn = RedisConnectionUtils.bindConnection(factory, enableTransactionSupport);  
        } else {  
            conn = RedisConnectionUtils.getConnection(factory);  
        }  
        boolean existingConnection = TransactionSynchronizationManager.hasResource(factory);  
        RedisConnection connToUse = preProcessConnection(conn, existingConnection);  
        boolean pipelineStatus = connToUse.isPipelined();  
        if (pipeline && !pipelineStatus) {  
            connToUse.openPipeline();  
        }  
        RedisConnection connToExpose = (exposeConnection ? connToUse : createRedisConnectionProxy(connToUse));  
        T result = action.doInRedis(connToExpose);  
        // close pipeline  
        if (pipeline && !pipelineStatus) {  
            connToUse.closePipeline();  
        }  
        // TODO: any other connection processing?  
        return postProcessResult(result, connToUse, existingConnection);  
    } finally {  
        if (!enableTransactionSupport) {  
            RedisConnectionUtils.releaseConnection(conn, factory);  
        }  
    }  
}

    这里面每次执行action.doInRedis(connToExpose)前都要调用RedisConnectionUtils.getConnection(factory);获得一个连接,进入RedisConnnectionUtils类中,getConnection(factory)最终调用的是doGetConnection(factory, true, false, enableTranactionSupport)这个函数。

这个函数我们可以看下api文档,发现实际上并不是真的创建一个新的redis连接,它只是在connectFactory中获取一个连接,也就是从连接池中取出一个连接。当然如果connectFactory没有连接可用,此时如果allowCreate=true便会创建出一个新的连接,并且加入到connectFactory中。

    这样就可以放心大胆的使用,而不用担心性能问题了。。。

posted @ 2022-01-27 18:35  zhangdaopin  阅读(1008)  评论(0编辑  收藏  举报