54 lines
1.6 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-21 10:17:40 +08:00
using Volo.Abp.DependencyInjection;
2025-04-07 16:44:25 +08:00
2025-04-17 20:28:50 +08:00
namespace JiShe.CollectBus.IoTDB.Provider
2025-04-07 16:44:25 +08:00
{
/// <summary>
/// 实现带缓存的Session工厂
/// </summary>
2025-04-21 10:17:40 +08:00
public class IoTDbSessionFactory : IIoTDbSessionFactory, ISingletonDependency
2025-04-07 16:44:25 +08:00
{
2025-04-21 10:17:40 +08:00
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
2025-04-21 10:17:40 +08:00
/// <summary>
/// IoTDbSessionFactory
/// </summary>
/// <param name="options"></param>
public IoTDbSessionFactory(IOptions<IoTDbOptions> options)
2025-04-07 16:44:25 +08:00
{
_options = options.Value;
}
2025-04-21 10:17:40 +08:00
public IIoTDbSessionPool GetSessionPool(bool useTableSession)
2025-04-07 16:44:25 +08:00
{
2025-04-21 10:17:40 +08:00
if (_disposed) throw new ObjectDisposedException(nameof(IoTDbSessionFactory));
2025-04-11 11:56:23 +08:00
2025-04-07 16:44:25 +08:00
return _pools.GetOrAdd(useTableSession, key =>
{
2025-04-11 11:56:23 +08:00
var pool = key
2025-04-21 10:17:40 +08:00
? (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
}
}