-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
♻ refactor(IDistributedCache): Optimize connections under Isolation a…
…nd Attempt to reconnect (#740) * ♻ refactor(IDistributedCache): Optimize connections under Isolation and Attempt to reconnect * chore: TargetFrameworks * refactor: Code optimization * chore: TargetFrameworks * ♻ refactor(IDistributedCache): Support multi tenant * ♻ refactor(IDistributedCache): Code smell * ♻ refactor(IDistributedCache): Adjust the GeneratedKey method
- Loading branch information
Showing
3 changed files
with
84 additions
and
6 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
...uildingBlocks/Caching/Masa.BuildingBlocks.Caching/Internal/DistributedCacheClientCache.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright (c) MASA Stack All rights reserved. | ||
// Licensed under the MIT License. See LICENSE.txt in the project root for license information. | ||
|
||
namespace Masa.BuildingBlocks.Caching; | ||
|
||
internal class DistributedCacheClientCache | ||
{ | ||
private static ConcurrentDictionary<string, IManualDistributedCacheClient> _cacheClients = new(); | ||
|
||
public IManualDistributedCacheClient GetCacheClient(IServiceProvider serviceProvider) | ||
{ | ||
var environment = GetCurrentEnvironment(serviceProvider); | ||
var tenantId = GetCurrentTenantId(serviceProvider); | ||
|
||
var key = GenerateKey(environment, tenantId); | ||
|
||
return _cacheClients.GetOrAdd(key, _ => CreateCacheClient(serviceProvider)); | ||
} | ||
|
||
private static string GetCurrentEnvironment(IServiceProvider serviceProvider) | ||
{ | ||
var multiEnvironmentContext = serviceProvider.GetService<IMultiEnvironmentContext>(); | ||
return multiEnvironmentContext?.CurrentEnvironment ?? string.Empty; | ||
} | ||
|
||
private static string GetCurrentTenantId(IServiceProvider serviceProvider) | ||
{ | ||
var multiTenantContext = serviceProvider.GetService<IMultiTenantContext>(); | ||
return multiTenantContext?.CurrentTenant?.Id ?? string.Empty; | ||
} | ||
|
||
private static string GenerateKey(string environment, string tenantId) | ||
{ | ||
if (string.IsNullOrEmpty(environment)) | ||
{ | ||
return tenantId; | ||
} | ||
if (string.IsNullOrEmpty(tenantId)) | ||
{ | ||
return environment; | ||
} | ||
return $"{environment}:{tenantId}"; | ||
} | ||
|
||
private static IManualDistributedCacheClient CreateCacheClient(IServiceProvider serviceProvider) | ||
{ | ||
try | ||
{ | ||
var scopedService = serviceProvider.GetRequiredService<ScopedService<IManualDistributedCacheClient>>(); | ||
return scopedService.Service; | ||
} | ||
catch (Exception ex) | ||
{ | ||
throw new InvalidOperationException("Failed to create cache client", ex); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters