Enhance SyncManager to raise events which can be handled on the server within hosted services. Raise create, update, delete events for all major entities. Include support for refresh and reload events to synchronize client state. Move client state cache invalidation to a hosted service to separate concerns and demonstrate events.
This commit is contained in:
45
Oqtane.Server/Infrastructure/CacheInvalidationService.cs
Normal file
45
Oqtane.Server/Infrastructure/CacheInvalidationService.cs
Normal file
@ -0,0 +1,45 @@
|
||||
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 EventJob : IHostedService
|
||||
{
|
||||
private readonly ISyncManager _syncManager;
|
||||
private readonly IMemoryCache _cache;
|
||||
|
||||
public EventJob(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;
|
||||
}
|
||||
}
|
||||
}
|
@ -6,8 +6,8 @@ namespace Oqtane.Infrastructure
|
||||
{
|
||||
public interface ISyncManager
|
||||
{
|
||||
event EventHandler<SyncEvent> EntityChanged;
|
||||
List<SyncEvent> GetSyncEvents(int tenantId, DateTime lastSyncDate);
|
||||
void AddSyncEvent(int tenantId, string entityName, int entityId);
|
||||
void AddSyncEvent(int tenantId, string entityName, int entityId, bool reload);
|
||||
void AddSyncEvent(int tenantId, string entityName, int entityId, string action);
|
||||
}
|
||||
}
|
||||
|
@ -1,21 +1,19 @@
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using Oqtane.Models;
|
||||
using Oqtane.Shared;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Oqtane.Infrastructure
|
||||
{
|
||||
public class SyncManager : ISyncManager
|
||||
{
|
||||
private readonly IMemoryCache _cache;
|
||||
private List<SyncEvent> SyncEvents { get; set; }
|
||||
|
||||
public SyncManager(IMemoryCache cache)
|
||||
public event EventHandler<SyncEvent> EntityChanged;
|
||||
|
||||
public SyncManager()
|
||||
{
|
||||
_cache = cache;
|
||||
SyncEvents = new List<SyncEvent>();
|
||||
}
|
||||
|
||||
@ -24,20 +22,22 @@ namespace Oqtane.Infrastructure
|
||||
return SyncEvents.Where(item => (item.TenantId == tenantId || item.TenantId == -1) && item.ModifiedOn >= lastSyncDate).ToList();
|
||||
}
|
||||
|
||||
public void AddSyncEvent(int tenantId, string entityName, int entityId)
|
||||
public void AddSyncEvent(int tenantId, string entityName, int entityId, string action)
|
||||
{
|
||||
AddSyncEvent(tenantId, entityName, entityId, false);
|
||||
}
|
||||
var syncevent = new SyncEvent { TenantId = tenantId, EntityName = entityName, EntityId = entityId, Action = action, ModifiedOn = DateTime.UtcNow };
|
||||
|
||||
public void AddSyncEvent(int tenantId, string entityName, int entityId, bool reload)
|
||||
{
|
||||
SyncEvents.Add(new SyncEvent { TenantId = tenantId, EntityName = entityName, EntityId = entityId, Reload = reload, ModifiedOn = DateTime.UtcNow });
|
||||
if (entityName == EntityNames.Site)
|
||||
{
|
||||
_cache.Remove($"site:{tenantId}:{entityId}");
|
||||
// client actions for PageState management
|
||||
if (action == SyncEventActions.Refresh || action == SyncEventActions.Reload)
|
||||
{
|
||||
// trim sync events
|
||||
SyncEvents.RemoveAll(item => item.ModifiedOn < DateTime.UtcNow.AddHours(-1));
|
||||
|
||||
// add sync event
|
||||
SyncEvents.Add(syncevent);
|
||||
}
|
||||
// trim sync events
|
||||
SyncEvents.RemoveAll(item => item.ModifiedOn < DateTime.UtcNow.AddHours(-1));
|
||||
|
||||
// raise event
|
||||
EntityChanged?.Invoke(this, syncevent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user