Browse Source

Optimize performance.

pull/11232/head
maliming 5 years ago
parent
commit
ee1fbf82ed
No known key found for this signature in database GPG Key ID: 96224957E51C89E
  1. 38
      framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/EventHandlerInvoker.cs
  2. 8
      framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/EventHandlerInvokerCacheItem.cs

38
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<string, IEventHandlerMethodExecutor> _cache;
private readonly ConcurrentDictionary<string, EventHandlerInvokerCacheItem> _cache;
public EventHandlerInvoker()
{
_cache = new ConcurrentDictionary<string, IEventHandlerMethodExecutor>();
_cache = new ConcurrentDictionary<string, EventHandlerInvokerCacheItem>();
}
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);
}

8
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; }
}
Loading…
Cancel
Save