日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

聊聊LettucePoolingConnectionProvider

發布時間:2025/7/25 编程问答 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 聊聊LettucePoolingConnectionProvider 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本文主要研究一下LettucePoolingConnectionProvider

LettucePoolingConnectionProvider

spring-data-redis-2.0.10.RELEASE-sources.jar!/org/springframework/data/redis/connection/lettuce/LettucePoolingConnectionProvider.java

/*** {@link LettuceConnectionProvider} with connection pooling support. This connection provider holds multiple pools (one* per connection type) for contextualized connection allocation.* <p />* Each allocated connection is tracked and to be returned into the pool which created the connection. Instances of this* class require {@link #destroy() disposal} to de-allocate lingering connections that were not returned to the pool and* to close the pools.** @author Mark Paluch* @author Christoph Strobl* @since 2.0* @see #getConnection(Class)*/ class LettucePoolingConnectionProvider implements LettuceConnectionProvider, RedisClientProvider, DisposableBean {private final static Log log = LogFactory.getLog(LettucePoolingConnectionProvider.class);private final LettuceConnectionProvider connectionProvider;private final GenericObjectPoolConfig poolConfig;private final Map<StatefulConnection<?, ?>, GenericObjectPool<StatefulConnection<?, ?>>> poolRef = new ConcurrentHashMap<>(32);private final Map<Class<?>, GenericObjectPool<StatefulConnection<?, ?>>> pools = new ConcurrentHashMap<>(32);LettucePoolingConnectionProvider(LettuceConnectionProvider connectionProvider,LettucePoolingClientConfiguration clientConfiguration) {Assert.notNull(connectionProvider, "ConnectionProvider must not be null!");Assert.notNull(clientConfiguration, "ClientConfiguration must not be null!");this.connectionProvider = connectionProvider;this.poolConfig = clientConfiguration.getPoolConfig();}/** (non-Javadoc)* @see org.springframework.data.redis.connection.lettuce.LettuceConnectionProvider#getConnection(java.lang.Class)*/@Overridepublic <T extends StatefulConnection<?, ?>> T getConnection(Class<T> connectionType) {GenericObjectPool<StatefulConnection<?, ?>> pool = pools.computeIfAbsent(connectionType, poolType -> {return ConnectionPoolSupport.createGenericObjectPool(() -> connectionProvider.getConnection(connectionType),poolConfig, false);});try {StatefulConnection<?, ?> connection = pool.borrowObject();poolRef.put(connection, pool);return connectionType.cast(connection);} catch (Exception e) {throw new PoolException("Could not get a resource from the pool", e);}}/** (non-Javadoc)* @see org.springframework.data.redis.connection.lettuce.RedisClientProvider#getRedisClient()*/@Overridepublic AbstractRedisClient getRedisClient() {if (connectionProvider instanceof RedisClientProvider) {return ((RedisClientProvider) connectionProvider).getRedisClient();}throw new IllegalStateException(String.format("Underlying connection provider %s does not implement RedisClientProvider!",connectionProvider.getClass().getName()));}/** (non-Javadoc)* @see org.springframework.data.redis.connection.lettuce.LettuceConnectionProvider#release(io.lettuce.core.api.StatefulConnection)*/@Overridepublic void release(StatefulConnection<?, ?> connection) {GenericObjectPool<StatefulConnection<?, ?>> pool = poolRef.remove(connection);if (pool == null) {throw new PoolException("Returned connection " + connection+ " was either previously returned or does not belong to this connection provider");}pool.returnObject(connection);}/** (non-Javadoc)* @see org.springframework.beans.factory.DisposableBean#destroy()*/@Overridepublic void destroy() throws Exception {if (!poolRef.isEmpty()) {log.warn("LettucePoolingConnectionProvider contains unreleased connections");poolRef.forEach((connection, pool) -> pool.returnObject(connection));poolRef.clear();}pools.forEach((type, pool) -> pool.close());pools.clear();} } 復制代碼
  • 這個provider與普通的連接不同,它區分了不同類型的連接池,因而pools是map結構的,而且還多了poolRef這個map來維護每個連接所在連接池
  • pools主要是用于根據連接類型獲取對應連接池,用于連接的借用場景,而poolRef主要是用于連接的歸還場景
  • 這里采用了ConcurrentHashMap的computeIfAbsent方法,對于key不存在的則觸發創建并返回,對于key存在的則直接返回

小結

  • lettuce 4.0版本引入了StatefulConnection,它會維護登錄、事務、所選數據庫,讀寫等狀態
  • StatefulConnection有幾個類型,分別是StatefulRedisConnection(StatefulRedisPubSubConnection、StatefulRedisMasterSlaveConnection)、StatefulRedisSentinelConnection、StatefulRedisClusterConnection
  • LettucePoolingConnectionProvider維護了多類型的連接的連接池,因而采用map的數據結構,不然單一類型的連接直接使用GenericObjectPool即可

doc

  • Stateful Connections

總結

以上是生活随笔為你收集整理的聊聊LettucePoolingConnectionProvider的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。