Skip to content

Commit

Permalink
Added init for ResourcesManager
Browse files Browse the repository at this point in the history
  • Loading branch information
dyatlov-a committed Sep 20, 2024
1 parent ed151d3 commit 6187472
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,15 @@ namespace Inc.TeamAssistant.Gateway.Services.Clients;
internal sealed class MessageProviderCached : IMessageProvider
{
private readonly IMemoryCache _memoryCache;
private readonly ILogger<MessageProviderCached> _logger;
private readonly IMessageProvider _messageProvider;
private readonly TimeSpan _cacheAbsoluteExpiration;

public MessageProviderCached(
IMemoryCache memoryCache,
ILogger<MessageProviderCached> logger,
IMessageProvider messageProvider,
TimeSpan cacheAbsoluteExpiration)
{
_memoryCache = memoryCache ?? throw new ArgumentNullException(nameof(memoryCache));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
_messageProvider = messageProvider ?? throw new ArgumentNullException(nameof(messageProvider));
_cacheAbsoluteExpiration = cacheAbsoluteExpiration;
}
Expand All @@ -32,13 +29,7 @@ public async Task<Dictionary<string, Dictionary<string, string>>> Get(Cancellati
c.SetAbsoluteExpiration(_cacheAbsoluteExpiration);
return await _messageProvider.Get(token);
});

if (cacheItem is null)
{
_logger.LogWarning("Can not get object with key {CacheKey} from cache", cacheKey);
return await _messageProvider.Get(token);
}

return cacheItem;
return cacheItem ?? await _messageProvider.Get(token);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,15 @@ namespace Inc.TeamAssistant.Gateway.Services.ServerCore;
internal sealed class QuickResponseCodeGeneratorCached : IQuickResponseCodeGenerator
{
private readonly IMemoryCache _memoryCache;
private readonly ILogger<QuickResponseCodeGeneratorCached> _logger;
private readonly IQuickResponseCodeGenerator _generator;
private readonly TimeSpan _cacheAbsoluteExpiration;

public QuickResponseCodeGeneratorCached(
IMemoryCache memoryCache,
ILogger<QuickResponseCodeGeneratorCached> logger,
IQuickResponseCodeGenerator generator,
TimeSpan cacheAbsoluteExpiration)
{
_memoryCache = memoryCache ?? throw new ArgumentNullException(nameof(memoryCache));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
_generator = generator ?? throw new ArgumentNullException(nameof(generator));
_cacheAbsoluteExpiration = cacheAbsoluteExpiration;
}
Expand All @@ -37,11 +34,7 @@ public string Generate(string data, string foreground, string background)

return _generator.Generate(data, foreground, background);
});

if (cacheItem is not null)
return cacheItem;

_logger.LogWarning("Can not get object with key {CacheKey} from cache", key);
return _generator.Generate(key, foreground, background);
return cacheItem ?? _generator.Generate(key, foreground, background);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@

private string _title = GlobalSettings.CanonicalName;

protected override async Task OnInitializedAsync()
{
await ResourcesManager.Initialize();
}

protected override void OnParametersSet()
{
_title = CreateTitle();
Expand Down
6 changes: 3 additions & 3 deletions src/Inc.TeamAssistant.WebUI/Features/Routes.razor
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@
{
SetCurrentLanguage();

NavigationManager.LocationChanged += OnLocationChanged;

await ResourcesManager.Load();
await ResourcesManager.Initialize();

_eventsProvider = ServiceProvider.GetService<EventsProvider>();
if (_eventsProvider is not null)
await _eventsProvider.Start();

NavigationManager.LocationChanged += OnLocationChanged;
}

private void OnLocationChanged(object? sender, LocationChangedEventArgs e) => SetCurrentLanguage();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public sealed class ResourcesManager : IDisposable
private readonly IMessageProvider _messageProvider;
private readonly PersistentComponentState _applicationState;

private readonly SemaphoreSlim _sync = new(1, 1);
private PersistingComponentStateSubscription? _persistingSubscription;
private Dictionary<string, Dictionary<string, string>> _resources = new();

Expand All @@ -21,10 +22,29 @@ public ResourcesManager(
_renderContext = renderContext ?? throw new ArgumentNullException(nameof(renderContext));
_applicationState = applicationState ?? throw new ArgumentNullException(nameof(applicationState));
}

public async Task Initialize(CancellationToken token = default)
{
if (_resources.Any())
return;

await _sync.WaitAsync(token);

try
{
if (!_resources.Any())
await Load(token);
}
finally
{
_sync.Release();
}
}

public async Task Load(CancellationToken token = default)
private async Task Load(CancellationToken token)
{
const string key = nameof(ResourcesManager);

var resources = _applicationState.TryTakeFromJson<Dictionary<string, Dictionary<string, string>>>(key, out var restored) && restored is not null
? restored
: await _messageProvider.Get(token);
Expand Down

0 comments on commit 6187472

Please sign in to comment.