Files
tiku-backend.net/Tiku.Infrastructure/Security/TenantFeatureCacheInvalidator.cs
xiong c497a3ca8d
Some checks failed
ci / release-gate (push) Has been cancelled
清理代码
2026-08-03 12:31:39 +08:00

44 lines
1.9 KiB
C#

using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Tiku.Application.Security;
using Tiku.Application.Tenancy;
namespace Tiku.Infrastructure.Security;
internal sealed class TenantFeatureCacheInvalidator(
IMemoryCache memoryCache,
IServiceProvider serviceProvider,
ITenantRuntimeCacheInvalidator runtimeCacheInvalidator,
ILogger<TenantFeatureCacheInvalidator> logger) : ITenantFeatureCacheInvalidator
{
public async Task InvalidateAsync(Guid tenantId, CancellationToken cancellationToken = default)
{
RemoveMemory(tenantId);
await runtimeCacheInvalidator.InvalidateAsync(tenantId, cancellationToken);
var distributedCache = serviceProvider.GetService<IDistributedCache>();
if (distributedCache is not null)
try
{
await Task.WhenAll(
distributedCache.RemoveAsync(
TenantFeatureSnapshotProvider.CacheKey(tenantId, FeatureAccessOperation.Read),
cancellationToken),
distributedCache.RemoveAsync(
TenantFeatureSnapshotProvider.CacheKey(tenantId, FeatureAccessOperation.Write),
cancellationToken));
}
catch (Exception exception) when (exception is not OperationCanceledException)
{
logger.LogWarning(exception,
"Tenant feature distributed cache invalidation failed for tenant {TenantId}.", tenantId);
}
}
private void RemoveMemory(Guid tenantId)
{
memoryCache.Remove(TenantFeatureSnapshotProvider.CacheKey(tenantId, FeatureAccessOperation.Read));
memoryCache.Remove(TenantFeatureSnapshotProvider.CacheKey(tenantId, FeatureAccessOperation.Write));
}
}