53 lines
1.5 KiB
C#
Raw Normal View History

2025-04-07 16:44:25 +08:00
using JiShe.CollectBus.IoTDBProvider.Interface;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace JiShe.CollectBus.IoTDBProvider.Provider
{
/// <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
}
}