using System.Collections.Concurrent; using JiShe.CollectBus.IoTDB.Interface; using JiShe.CollectBus.IoTDB.Options; using Microsoft.Extensions.Options; namespace JiShe.CollectBus.IoTDB.Provider { /// /// 实现带缓存的Session工厂 /// public class IoTDBSessionFactory : IIoTDBSessionFactory { private readonly IoTDBOptions _options; private readonly ConcurrentDictionary _pools = new(); private bool _disposed; public IoTDBSessionFactory(IOptions options) { _options = options.Value; } public IIoTDBSessionPool GetSessionPool(bool useTableSession) { if (_disposed) throw new ObjectDisposedException(nameof(IoTDBSessionFactory)); return _pools.GetOrAdd(useTableSession, key => { var pool = key ? (IIoTDBSessionPool)new TableSessionPoolAdapter(_options) : new SessionPoolAdapter(_options); pool.OpenAsync().ConfigureAwait(false).GetAwaiter().GetResult(); ; return pool; }); } public void Dispose() { foreach (var pool in _pools.Values) { pool.Dispose(); } _pools.Clear(); _disposed = true; } } }