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中。

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

  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值