forked from xiongyuxing/tiku-backend.net
refactor: consolidate backend into modular monolith
This commit is contained in:
@@ -96,17 +96,6 @@ internal sealed class RedisSecurityStore(
|
||||
}
|
||||
}
|
||||
|
||||
public async Task SetInvalidationVersionAsync(
|
||||
string realm,
|
||||
Guid? tenantId,
|
||||
Guid? userId,
|
||||
long version,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
var key = $"{prefix}:auth-inv:{Normalize(realm)}:{tenantId?.ToString("N") ?? "-"}:{userId?.ToString("N") ?? "-"}";
|
||||
await connection.GetDatabase().StringSetAsync(key, version, TimeSpan.FromDays(2)).WaitAsync(cancellationToken);
|
||||
}
|
||||
|
||||
private static string Normalize(string value) =>
|
||||
string.Concat(value.Trim().ToLowerInvariant().Select(character =>
|
||||
char.IsLetterOrDigit(character) || character is '-' or '_' ? character : '-'));
|
||||
@@ -122,13 +111,6 @@ public sealed class NullRedisSecurityStore : IRedisSecurityStore
|
||||
Task.FromResult(new DistributedRateLimitResult(true));
|
||||
|
||||
public Task<bool> PingAsync(CancellationToken cancellationToken = default) => Task.FromResult(false);
|
||||
|
||||
public Task SetInvalidationVersionAsync(
|
||||
string realm,
|
||||
Guid? tenantId,
|
||||
Guid? userId,
|
||||
long version,
|
||||
CancellationToken cancellationToken = default) => Task.CompletedTask;
|
||||
}
|
||||
|
||||
public sealed class RedisSecurityUnavailableException(Exception innerException)
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
using Microsoft.Extensions.Caching.Distributed;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using StackExchange.Redis;
|
||||
using Tiku.Application.Security;
|
||||
using Tiku.Application.Tenancy;
|
||||
|
||||
@@ -13,11 +11,8 @@ internal sealed class TenantFeatureCacheInvalidator(
|
||||
IMemoryCache memoryCache,
|
||||
IServiceProvider serviceProvider,
|
||||
ITenantRuntimeCacheInvalidator runtimeCacheInvalidator,
|
||||
ILogger<TenantFeatureCacheInvalidator> logger) : ITenantFeatureCacheInvalidator, IHostedService
|
||||
ILogger<TenantFeatureCacheInvalidator> logger) : ITenantFeatureCacheInvalidator
|
||||
{
|
||||
private const string ChannelName = "tiku:tenant-feature-snapshot:invalidate:v1";
|
||||
private ISubscriber? subscriber;
|
||||
|
||||
public async Task InvalidateAsync(Guid tenantId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
RemoveMemory(tenantId);
|
||||
@@ -36,47 +31,6 @@ internal sealed class TenantFeatureCacheInvalidator(
|
||||
logger.LogWarning(exception, "Tenant feature distributed cache invalidation failed for tenant {TenantId}.", tenantId);
|
||||
}
|
||||
}
|
||||
|
||||
var connection = serviceProvider.GetService<IConnectionMultiplexer>();
|
||||
if (connection is not null)
|
||||
{
|
||||
try
|
||||
{
|
||||
await connection.GetSubscriber()
|
||||
.PublishAsync(RedisChannel.Literal(ChannelName), tenantId.ToString("N"))
|
||||
.WaitAsync(cancellationToken);
|
||||
}
|
||||
catch (Exception exception) when (exception is RedisException or TimeoutException)
|
||||
{
|
||||
logger.LogWarning(exception, "Tenant feature L1 invalidation broadcast failed for tenant {TenantId}.", tenantId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
var connection = serviceProvider.GetService<IConnectionMultiplexer>();
|
||||
if (connection is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
subscriber = connection.GetSubscriber();
|
||||
await subscriber.SubscribeAsync(RedisChannel.Literal(ChannelName), (_, value) =>
|
||||
{
|
||||
if (Guid.TryParseExact(value.ToString(), "N", out var tenantId))
|
||||
{
|
||||
RemoveMemory(tenantId);
|
||||
}
|
||||
}).WaitAsync(cancellationToken);
|
||||
}
|
||||
|
||||
public async Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (subscriber is not null)
|
||||
{
|
||||
await subscriber.UnsubscribeAsync(RedisChannel.Literal(ChannelName)).WaitAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
private void RemoveMemory(Guid tenantId)
|
||||
|
||||
Reference in New Issue
Block a user