Skip to content

Commit

Permalink
3.2.3 新增了清除缓存的功能,当无法从缓存中得到监听器信息时,会清除缓存重新发布。若再次发布依然找不到监听器信息则抛出异常
Browse files Browse the repository at this point in the history
  • Loading branch information
ShimizuShiori committed Apr 21, 2020
1 parent 2822988 commit 0c7d593
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 13 deletions.
9 changes: 5 additions & 4 deletions src/DotNet/Reface.EventBus/DefaultCache.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Reface.EventBus
{
public class DefaultCache : ICache
{
private static readonly ConcurrentDictionary<String, Object> values = new ConcurrentDictionary<string, object>();

public void Clean(string name)
{
values.TryRemove(name, out object value);
}

public T GetOrCreate<T>(string name, Func<T> factory)
{
return (T)values.GetOrAdd(name, s => factory());
Expand Down
28 changes: 26 additions & 2 deletions src/DotNet/Reface.EventBus/DefaultEventBus.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Reface.EventBus.EventListenerFinders;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection.Emit;

Expand Down Expand Up @@ -31,12 +32,33 @@ public DefaultEventBus() : this(new ConfigurationEventListenerFinder())

public void Publish(Event @event)
{
this.Publish(@event, 0);
}

public void Publish(Event @event, int time)
{

var allListeners = this.eventListenerFinder.CreateAllEventListeners();
allListeners = this.SortEventListeners(allListeners);
Type eventType = @event.GetType();
Dictionary<Type, ListenerInfo> infoMap = cache.GetOrCreate($"ListenerInfosOf${eventType.FullName}",
string cacheKey = $"ListenerInfosOf${eventType.FullName}";
Dictionary<Type, ListenerInfo> infoMap = cache.GetOrCreate(cacheKey,
() => CreateListenerInfos(eventType, allListeners));

foreach (var listener in allListeners)
{
Type listenerType = listener.GetType();
if (!infoMap.ContainsKey(listenerType))
{
if (time != 0)
throw new KeyNotFoundException($"未发现监听器 [{listenerType.FullName}] 的信息,经清除缓存后,依然无法正常发布。");

Debug.WriteLine($"未发现监听器 [{listenerType.FullName}] 的信息,可能与缓存有关,清除缓存将重新发布");
cache.Clean(cacheKey);
this.Publish(@event, time + 1);
}
}

foreach (var listener in allListeners)
{
Type listenerType = listener.GetType();
Expand All @@ -50,6 +72,8 @@ public void Publish(Event @event)
}
}



/// <summary>
/// Sort all EventListeners b Priority
/// </summary>
Expand All @@ -71,7 +95,7 @@ private IEnumerable<IEventListener> SortEventListeners(IEnumerable<IEventListene
.Select(x => x.L)
.ToList();
}

private Dictionary<Type, ListenerInfo> CreateListenerInfos(Type eventType, IEnumerable<IEventListener> allListeners)
{
Dictionary<Type, ListenerInfo> result = new Dictionary<Type, ListenerInfo>();
Expand Down
2 changes: 2 additions & 0 deletions src/DotNet/Reface.EventBus/ICache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ namespace Reface.EventBus
public interface ICache
{
T GetOrCreate<T>(String name, Func<T> factory);

void Clean(string name);
}
}
10 changes: 3 additions & 7 deletions src/DotNet/Reface.EventBus/package.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
<!--*-->
<id>Reface.EventBus</id>
<!--*-->
<version>3.2.0</version>
<version>3.2.3</version>
<title>Reface.EventBus</title>
<!--*-->
<authors>ShimizuShiori</authors>
<owners>ShimizuShiori</owners>
<authors>Felix</authors>
<owners>Felix</owners>
<licenseUrl>https://github.com/ShimizuShiori/EventBus/blob/master/LICENSE</licenseUrl>
<projectUrl>https://github.com/ShimizuShiori/EventBus</projectUrl>
<!--
Expand All @@ -18,10 +18,6 @@
<!--*-->
<description>EventBus in C#</description>
<!--*-->
<releaseNotes>
+ 添加验证,事件源不能为 null
+ 当发布消息时,才获取所有事件监听者
</releaseNotes>
<copyright>Copyright © HP Inc. 2019</copyright>
<tags>
</tags>
Expand Down

0 comments on commit 0c7d593

Please sign in to comment.