Merge remote-tracking branch 'oqtane/dev' into dev
This commit is contained in:
		@ -0,0 +1,24 @@
 | 
			
		||||
using Microsoft.Extensions.Caching.Memory;
 | 
			
		||||
using Oqtane.Models;
 | 
			
		||||
using Oqtane.Shared;
 | 
			
		||||
 | 
			
		||||
namespace Oqtane.Infrastructure.EventSubscribers
 | 
			
		||||
{
 | 
			
		||||
    public class CacheInvalidationEventSubscriber : IEventSubscriber
 | 
			
		||||
    {
 | 
			
		||||
        private readonly IMemoryCache _cache;
 | 
			
		||||
 | 
			
		||||
        public CacheInvalidationEventSubscriber(IMemoryCache cache)
 | 
			
		||||
        {
 | 
			
		||||
            _cache = cache;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public void EntityChanged(SyncEvent syncEvent)
 | 
			
		||||
        {
 | 
			
		||||
            if (syncEvent.EntityName == "Site" && syncEvent.Action == SyncEventActions.Refresh)
 | 
			
		||||
            {
 | 
			
		||||
                _cache.Remove($"site:{syncEvent.TenantId}:{syncEvent.EntityId}");
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -1,45 +0,0 @@
 | 
			
		||||
using System.Threading.Tasks;
 | 
			
		||||
using System.Threading;
 | 
			
		||||
using Microsoft.Extensions.Hosting;
 | 
			
		||||
using Oqtane.Models;
 | 
			
		||||
using Microsoft.Extensions.Caching.Memory;
 | 
			
		||||
using Oqtane.Shared;
 | 
			
		||||
 | 
			
		||||
namespace Oqtane.Infrastructure
 | 
			
		||||
{
 | 
			
		||||
    public class CacheInvalidationHostedService : IHostedService
 | 
			
		||||
    {
 | 
			
		||||
        private readonly ISyncManager _syncManager;
 | 
			
		||||
        private readonly IMemoryCache _cache;
 | 
			
		||||
 | 
			
		||||
        public CacheInvalidationHostedService(ISyncManager syncManager, IMemoryCache cache)
 | 
			
		||||
        {
 | 
			
		||||
            _syncManager = syncManager;
 | 
			
		||||
            _cache = cache;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        void EntityChanged(object sender, SyncEvent e)
 | 
			
		||||
        {
 | 
			
		||||
            if (e.EntityName == "Site" && e.Action == SyncEventActions.Refresh)
 | 
			
		||||
            {
 | 
			
		||||
                _cache.Remove($"site:{e.TenantId}:{e.EntityId}");
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public Task StartAsync(CancellationToken cancellationToken)
 | 
			
		||||
        {
 | 
			
		||||
            _syncManager.EntityChanged += EntityChanged;
 | 
			
		||||
 | 
			
		||||
            return Task.CompletedTask;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public Task StopAsync(CancellationToken cancellationToken)
 | 
			
		||||
        {
 | 
			
		||||
            return Task.CompletedTask;
 | 
			
		||||
        }
 | 
			
		||||
        public void Dispose()
 | 
			
		||||
        {
 | 
			
		||||
            _syncManager.EntityChanged -= EntityChanged;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -0,0 +1,80 @@
 | 
			
		||||
using System.Threading.Tasks;
 | 
			
		||||
using System.Threading;
 | 
			
		||||
using Microsoft.Extensions.Hosting;
 | 
			
		||||
using Oqtane.Models;
 | 
			
		||||
using System;
 | 
			
		||||
using System.Reflection;
 | 
			
		||||
using Microsoft.Extensions.DependencyInjection;
 | 
			
		||||
using Microsoft.Extensions.Caching.Memory;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using Microsoft.Extensions.Logging;
 | 
			
		||||
using Oqtane.Shared;
 | 
			
		||||
 | 
			
		||||
namespace Oqtane.Infrastructure
 | 
			
		||||
{
 | 
			
		||||
    public class EventDistributorHostedService : IHostedService
 | 
			
		||||
    {
 | 
			
		||||
        private readonly IServiceProvider _serviceProvider;
 | 
			
		||||
        private readonly ISyncManager _syncManager;
 | 
			
		||||
        private readonly IMemoryCache _cache;
 | 
			
		||||
        private readonly ILogger<EventDistributorHostedService> _filelogger;
 | 
			
		||||
 | 
			
		||||
        public EventDistributorHostedService(IServiceProvider serviceProvider, ISyncManager syncManager, IMemoryCache cache, ILogger<EventDistributorHostedService> filelogger)
 | 
			
		||||
        {
 | 
			
		||||
            _serviceProvider = serviceProvider;
 | 
			
		||||
            _syncManager = syncManager;
 | 
			
		||||
            _cache = cache;
 | 
			
		||||
            _filelogger = filelogger;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        void EntityChanged(object sender, SyncEvent syncEvent)
 | 
			
		||||
        {
 | 
			
		||||
            List<Type> eventSubscribers = _cache.GetOrCreate($"eventsubscribers", entry =>
 | 
			
		||||
            {
 | 
			
		||||
                eventSubscribers = new List<Type>();
 | 
			
		||||
                var assemblies = AppDomain.CurrentDomain.GetOqtaneAssemblies();
 | 
			
		||||
                foreach (Assembly assembly in assemblies)
 | 
			
		||||
                {
 | 
			
		||||
                    foreach (var type in assembly.GetTypes(typeof(IEventSubscriber)))
 | 
			
		||||
                    {
 | 
			
		||||
                        eventSubscribers.Add(type);
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
                entry.Priority = CacheItemPriority.NeverRemove;
 | 
			
		||||
                return eventSubscribers;
 | 
			
		||||
            });
 | 
			
		||||
 | 
			
		||||
            foreach (var type in eventSubscribers)
 | 
			
		||||
            {
 | 
			
		||||
                try
 | 
			
		||||
                {
 | 
			
		||||
                    var obj = ActivatorUtilities.CreateInstance(_serviceProvider, type) as IEventSubscriber;
 | 
			
		||||
                    if (obj != null)
 | 
			
		||||
                    {
 | 
			
		||||
                        obj.EntityChanged(syncEvent);
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
                catch (Exception ex)
 | 
			
		||||
                {
 | 
			
		||||
                    _filelogger.LogError(Utilities.LogMessage(this, $"Error In EventSubscriber {type.AssemblyQualifiedName} - {ex.Message}"));
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public Task StartAsync(CancellationToken cancellationToken)
 | 
			
		||||
        {
 | 
			
		||||
            _syncManager.EntityChanged += EntityChanged;
 | 
			
		||||
 | 
			
		||||
            return Task.CompletedTask;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public Task StopAsync(CancellationToken cancellationToken)
 | 
			
		||||
        {
 | 
			
		||||
            return Task.CompletedTask;
 | 
			
		||||
        }
 | 
			
		||||
        public void Dispose()
 | 
			
		||||
        {
 | 
			
		||||
            _syncManager.EntityChanged -= EntityChanged;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -0,0 +1,9 @@
 | 
			
		||||
using Oqtane.Models;
 | 
			
		||||
 | 
			
		||||
namespace Oqtane.Infrastructure
 | 
			
		||||
{
 | 
			
		||||
    public interface IEventSubscriber
 | 
			
		||||
    {
 | 
			
		||||
        void EntityChanged(SyncEvent syncEvent);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -61,6 +61,6 @@
 | 
			
		||||
  </Target>
 | 
			
		||||
  <ItemGroup>
 | 
			
		||||
    <!-- extends watching group to include *.dll files and exclude the ones cause an infinite loop -->
 | 
			
		||||
    <Watch Include="**\*.dll" Exclude="**\Microsoft.EntityFrameworkCore.*.dll;**\Oqtane.Database.*.dll;"/>
 | 
			
		||||
    <Watch Include="**\*.dll" Exclude="**\Microsoft.EntityFrameworkCore.*.dll;**\Oqtane.Database.*.dll;" />
 | 
			
		||||
  </ItemGroup>
 | 
			
		||||
</Project>
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user