Skip to content

Commit

Permalink
♻ refactor(IDistributedCache): Support multi tenant
Browse files Browse the repository at this point in the history
  • Loading branch information
wzh425 committed Nov 25, 2024
1 parent a25efeb commit 834b00c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,49 @@ namespace Masa.BuildingBlocks.Caching;

internal class DistributedCacheClientCache
{
public static ConcurrentDictionary<string, IManualDistributedCacheClient> CacheClients { get; set; } = new();
private readonly ConcurrentDictionary<string, IManualDistributedCacheClient> _cacheClients = new();

public IManualDistributedCacheClient GetCacheClient(IServiceProvider serviceProvider)
{
var multiEnvironmentContext = serviceProvider.GetRequiredService<IMultiEnvironmentContext>();
var environment = multiEnvironmentContext.CurrentEnvironment;
var environment = GetCurrentEnvironment(serviceProvider);
var tenantId = GetCurrentTenantId(serviceProvider);

return CacheClients.GetOrAdd(environment, env =>
var key = GenerateKey(environment, tenantId);

return _cacheClients.GetOrAdd(key, _ => CreateCacheClient(serviceProvider));
}

private string GetCurrentEnvironment(IServiceProvider serviceProvider)
{
var multiEnvironmentContext = serviceProvider.GetService<IMultiEnvironmentContext>();
return multiEnvironmentContext?.CurrentEnvironment ?? string.Empty;
}

private string GetCurrentTenantId(IServiceProvider serviceProvider)
{
var multiTenantContext = serviceProvider.GetService<IMultiTenantContext>();
return multiTenantContext?.CurrentTenant?.Id ?? string.Empty;
}

private string GenerateKey(string environment, string tenantId)
{
if (string.IsNullOrEmpty(tenantId))
{
return environment;
}
return $"{environment}:{tenantId}";
}

private IManualDistributedCacheClient CreateCacheClient(IServiceProvider serviceProvider)
{
try
{
var scopedService = serviceProvider.GetRequiredService<ScopedService<IManualDistributedCacheClient>>();
return scopedService.Service;
}
catch (Exception ex)
{
var cacheClient = serviceProvider.GetRequiredService<ScopedService<IManualDistributedCacheClient>>().Service;
return cacheClient;
});
throw new InvalidOperationException("Failed to create cache client", ex);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ protected IDatabase Db
{
get
{
return EnsureDbConnection();
EnsureDbConnection();

return _connection.GetDatabase();
}
}

Expand Down Expand Up @@ -51,23 +53,17 @@ private RedisCacheClientBase(
GlobalJsonSerializerOptions = jsonSerializerOptions ?? new JsonSerializerOptions().EnableDynamicTypes();
}

protected IDatabase EnsureDbConnection()
protected void EnsureDbConnection()
{
if (_connection.IsConnected || _connection.IsConnecting)
if (!_connection.IsConnected && !_connection.IsConnecting)
{
return _connection.GetDatabase();
// Attempt to reconnect
var redisConfiguration = _redisConfigurationOptions.GetAvailableRedisOptions();
_connection = ConnectionMultiplexer.Connect(redisConfiguration);
Subscriber = _connection.GetSubscriber();
}

// Attempt to reconnect
var redisConfiguration = _redisConfigurationOptions.GetAvailableRedisOptions();
_connection = ConnectionMultiplexer.Connect(redisConfiguration);
Subscriber = _connection.GetSubscriber();

if (_connection.IsConnected || _connection.IsConnecting)
{
return _connection.GetDatabase();
}
else
if (!_connection.IsConnected && !_connection.IsConnecting)
{
throw new NotSupportedException("Unable to reconnect to Redis, please check the connection settings and try again.");
}
Expand Down

0 comments on commit 834b00c

Please sign in to comment.