Skip to content

Commit 64a66b5

Browse files
committed
简化队列事件总线
1 parent 8fcdde2 commit 64a66b5

File tree

2 files changed

+5
-15
lines changed

2 files changed

+5
-15
lines changed

NewLife.Core/Caching/MemoryCache.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ public override ICollection<T> GetSet<T>(String key)
448448
public override IEventBus<T> GetEventBus<T>(String topic, String clientId = "")
449449
{
450450
var key = $"eventbus:{topic}";
451-
var item = GetOrAddItem(key, k => new QueueEventBus<T>(this, topic, clientId));
451+
var item = GetOrAddItem(key, k => new QueueEventBus<T>(this, topic));
452452
return item.Visit<IEventBus<T>>() ??
453453
throw new InvalidCastException($"Unable to convert the value of [{topic}] from {item.TypeCode} to {typeof(IEventBus<T>)}");
454454
}

NewLife.Core/Caching/QueueEventBus.cs

+4-14
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,11 @@
55
namespace NewLife.Caching;
66

77
/// <summary>消息队列事件总线。通过消息队列来发布和订阅消息</summary>
8-
public class QueueEventBus<TEvent> : EventBus<TEvent>
8+
/// <remarks>实例化消息队列事件总线</remarks>
9+
public class QueueEventBus<TEvent>(ICache cache, String topic) : EventBus<TEvent>
910
{
1011
private IProducerConsumer<TEvent>? _queue;
11-
private readonly ICache _cache;
12-
private readonly String _topic;
13-
private readonly String _clientId;
14-
CancellationTokenSource? _source;
15-
16-
/// <summary>实例化消息队列事件总线</summary>
17-
public QueueEventBus(ICache cache, String topic, String clientId)
18-
{
19-
_cache = cache;
20-
_topic = topic;
21-
_clientId = clientId;
22-
}
12+
private CancellationTokenSource? _source;
2313

2414
/// <summary>销毁</summary>
2515
/// <param name="disposing"></param>
@@ -36,7 +26,7 @@ protected virtual void Init()
3626
{
3727
if (_queue != null) return;
3828

39-
_queue = _cache.GetQueue<TEvent>(_topic);
29+
_queue = cache.GetQueue<TEvent>(topic);
4030
}
4131

4232
/// <summary>发布消息到消息队列</summary>

0 commit comments

Comments
 (0)