RedisEventSourceStorer.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using System.Threading.Channels;
  2. namespace Admin.NET.Core;
  3. /// <summary>
  4. /// Redis自定义事件源存储器
  5. /// </summary>
  6. public sealed class RedisEventSourceStorer : IEventSourceStorer, IDisposable
  7. {
  8. /// <summary>
  9. /// 消费者
  10. /// </summary>
  11. private readonly EventConsumer<ChannelEventSource> _eventConsumer;
  12. /// <summary>
  13. /// 内存通道事件源存储器
  14. /// </summary>
  15. private readonly Channel<IEventSource> _channel;
  16. /// <summary>
  17. /// Redis 连接对象
  18. /// </summary>
  19. private readonly FullRedis _redis;
  20. /// <summary>
  21. /// 路由键
  22. /// </summary>
  23. private readonly string _routeKey;
  24. /// <summary>
  25. /// 构造函数
  26. /// </summary>
  27. /// <param name="redis">Redis 连接对象</param>
  28. /// <param name="routeKey">路由键</param>
  29. /// <param name="capacity">存储器最多能够处理多少消息,超过该容量进入等待写入</param>
  30. public RedisEventSourceStorer(ICache redis, string routeKey, int capacity)
  31. {
  32. // 配置通道,设置超出默认容量后进入等待
  33. var boundedChannelOptions = new BoundedChannelOptions(capacity)
  34. {
  35. FullMode = BoundedChannelFullMode.Wait
  36. };
  37. // 创建有限容量通道
  38. _channel = Channel.CreateBounded<IEventSource>(boundedChannelOptions);
  39. _redis = redis as FullRedis;
  40. _routeKey = routeKey;
  41. // 创建消息订阅者
  42. _eventConsumer = new EventConsumer<ChannelEventSource>(_redis, _routeKey);
  43. // 订阅消息写入 Channel
  44. _eventConsumer.Received += (send, cr) =>
  45. {
  46. // 反序列化消息
  47. //var eventSource = JsonConvert.DeserializeObject<ChannelEventSource>(cr);
  48. // 写入内存管道存储器
  49. _channel.Writer.WriteAsync(cr);
  50. };
  51. // 启动消费者
  52. _eventConsumer.Start();
  53. }
  54. /// <summary>
  55. /// 将事件源写入存储器
  56. /// </summary>
  57. /// <param name="eventSource">事件源对象</param>
  58. /// <param name="cancellationToken">取消任务 Token</param>
  59. /// <returns><see cref="ValueTask"/></returns>
  60. public async ValueTask WriteAsync(IEventSource eventSource, CancellationToken cancellationToken)
  61. {
  62. // 空检查
  63. if (eventSource == default)
  64. {
  65. throw new ArgumentNullException(nameof(eventSource));
  66. }
  67. // 这里判断是否是 ChannelEventSource 或者 自定义的 EventSource
  68. if (eventSource is ChannelEventSource source)
  69. {
  70. // 序列化消息
  71. //var data = JsonSerializer.Serialize(source);
  72. // 获取一个订阅对象
  73. var queue = _redis.GetQueue<ChannelEventSource>(_routeKey);
  74. // 异步发布
  75. await Task.Factory.StartNew(() =>
  76. {
  77. queue.Add(source);
  78. }, cancellationToken, TaskCreationOptions.LongRunning, System.Threading.Tasks.TaskScheduler.Default);
  79. }
  80. else
  81. {
  82. // 这里处理动态订阅问题
  83. await _channel.Writer.WriteAsync(eventSource, cancellationToken);
  84. }
  85. }
  86. /// <summary>
  87. /// 从存储器中读取一条事件源
  88. /// </summary>
  89. /// <param name="cancellationToken">取消任务 Token</param>
  90. /// <returns>事件源对象</returns>
  91. public async ValueTask<IEventSource> ReadAsync(CancellationToken cancellationToken)
  92. {
  93. // 读取一条事件源
  94. var eventSource = await _channel.Reader.ReadAsync(cancellationToken);
  95. return eventSource;
  96. }
  97. /// <summary>
  98. /// 释放非托管资源
  99. /// </summary>
  100. public async void Dispose()
  101. {
  102. await _eventConsumer.Stop();
  103. GC.SuppressFinalize(this);
  104. }
  105. }