49 lines
1.4 KiB
C#
Raw Normal View History

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