From ee1fbf82ed12dc0cb371cda1c7602594c6fae639 Mon Sep 17 00:00:00 2001 From: maliming Date: Mon, 10 Jan 2022 21:38:06 +0800 Subject: [PATCH] Optimize performance. --- .../Volo/Abp/EventBus/EventHandlerInvoker.cs | 38 ++++++++++++------- .../EventBus/EventHandlerInvokerCacheItem.cs | 8 ++++ 2 files changed, 32 insertions(+), 14 deletions(-) create mode 100644 framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/EventHandlerInvokerCacheItem.cs diff --git a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/EventHandlerInvoker.cs b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/EventHandlerInvoker.cs index a75b1bb3fa..5c7517cfe1 100644 --- a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/EventHandlerInvoker.cs +++ b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/EventHandlerInvoker.cs @@ -8,33 +8,43 @@ namespace Volo.Abp.EventBus; public class EventHandlerInvoker : IEventHandlerInvoker, ISingletonDependency { - private readonly ConcurrentDictionary _cache; + private readonly ConcurrentDictionary _cache; public EventHandlerInvoker() { - _cache = new ConcurrentDictionary(); + _cache = new ConcurrentDictionary(); } public async Task InvokeAsync(IEventHandler eventHandler, object eventData, Type eventType) { - var notAnEventHandler = true; - if (typeof(ILocalEventHandler<>).MakeGenericType(eventType).IsInstanceOfType(eventHandler)) + var cacheItem = _cache.GetOrAdd($"{eventHandler.GetType().FullName}-{eventType.FullName}", _ => { - var eventHandlerCall = _cache.GetOrAdd($"{typeof(LocalEventHandlerMethodExecutor<>).FullName}{eventHandler.GetType().FullName}-{eventType.FullName}", - (_) => (IEventHandlerMethodExecutor)Activator.CreateInstance(typeof(LocalEventHandlerMethodExecutor<>).MakeGenericType(eventType))); - await eventHandlerCall.ExecutorAsync(eventHandler, eventData); - notAnEventHandler = false; + var item = new EventHandlerInvokerCacheItem(); + + if (typeof(ILocalEventHandler<>).MakeGenericType(eventType).IsInstanceOfType(eventHandler)) + { + item.Local = (IEventHandlerMethodExecutor)Activator.CreateInstance(typeof(LocalEventHandlerMethodExecutor<>).MakeGenericType(eventType)); + } + + if (typeof(IDistributedEventHandler<>).MakeGenericType(eventType).IsInstanceOfType(eventHandler)) + { + item.Distributed = (IEventHandlerMethodExecutor)Activator.CreateInstance(typeof(DistributedEventHandlerMethodExecutor<>).MakeGenericType(eventType)); + } + + return item; + }); + + if (cacheItem.Local != null) + { + await cacheItem.Local.ExecutorAsync(eventHandler, eventData); } - if (typeof(IDistributedEventHandler<>).MakeGenericType(eventType).IsInstanceOfType(eventHandler)) + if (cacheItem.Distributed != null) { - var eventHandlerCall = _cache.GetOrAdd($"{typeof(DistributedEventHandlerMethodExecutor<>).FullName}{eventHandler.GetType().FullName}-{eventType.FullName}", - (_) => (IEventHandlerMethodExecutor)Activator.CreateInstance(typeof(DistributedEventHandlerMethodExecutor<>).MakeGenericType(eventType))); - await eventHandlerCall.ExecutorAsync(eventHandler, eventData); - notAnEventHandler = false; + await cacheItem.Distributed.ExecutorAsync(eventHandler, eventData); } - if (notAnEventHandler) + if (cacheItem.Local == null && cacheItem.Distributed == null) { throw new AbpException("The object instance is not an event handler. Object type: " + eventHandler.GetType().AssemblyQualifiedName); } diff --git a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/EventHandlerInvokerCacheItem.cs b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/EventHandlerInvokerCacheItem.cs new file mode 100644 index 0000000000..0ec617afca --- /dev/null +++ b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/EventHandlerInvokerCacheItem.cs @@ -0,0 +1,8 @@ +namespace Volo.Abp.EventBus; + +public class EventHandlerInvokerCacheItem +{ + public IEventHandlerMethodExecutor Local { get; set; } + + public IEventHandlerMethodExecutor Distributed { get; set; } +}