Skip to content

Commit

Permalink
fix: GetLanguagesHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
loyldg committed Dec 13, 2024
1 parent b5b4c4e commit 86902de
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ internal sealed class GetLanguagesHandler(ILanguageCacheService languageCacheSer
protected override async Task<TVector<ILangPackLanguage>> HandleCoreAsync(IRequestInput input,
RequestGetLanguages obj)
{
var languageReadModels = await languageCacheService.GetAllLanguagesAsync();
var langPack = obj.LangPack;
if (string.IsNullOrEmpty(langPack))
{
langPack = input.DeviceType.ToString().ToLower();
}
var languageReadModels = await languageCacheService.GetAllLanguagesAsync(langPack);
var languages = new TVector<ILangPackLanguage>();
foreach (var languageReadModel in languageReadModels)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace MyTelegram.Messenger.Services.Caching;
using System.Collections.Frozen;

namespace MyTelegram.Messenger.Services.Caching;

public class LanguageTextItem(string key, string value, int languageVersion)
{
Expand All @@ -11,7 +13,7 @@ public interface ILanguageCacheService
{
Task LoadAllLanguagesAsync();
Task LoadAllLanguageTextAsync();
Task<IReadOnlyCollection<ILanguageReadModel>> GetAllLanguagesAsync();
Task<IReadOnlyCollection<ILanguageReadModel>> GetAllLanguagesAsync(string languagePack);

Task<IReadOnlyCollection<LanguageTextItem>> GetLanguageTextAsync(string languageCode,
string languagePack);
Expand All @@ -28,13 +30,16 @@ Task<IReadOnlyCollection<LanguageTextItem>> GetLanguageTextAsync(string language

public class LanguageCacheService(IQueryProcessor queryProcessor, ILogger<LanguageCacheService> logger) : ILanguageCacheService, ISingletonDependency
{
private Dictionary<string, ILanguageReadModel> _languageReadModels = [];
private Dictionary<string, Dictionary<string, LanguageTextItem>> _languageTexts = [];
private FrozenDictionary<string, FrozenDictionary<string, ILanguageReadModel>> _languageReadModels = new Dictionary<string, FrozenDictionary<string, ILanguageReadModel>>().ToFrozenDictionary();
private FrozenDictionary<string, Dictionary<string, LanguageTextItem>> _languageTexts = new Dictionary<string, Dictionary<string, LanguageTextItem>>().ToFrozenDictionary();

public async Task LoadAllLanguagesAsync()
{
var languages = await queryProcessor.ProcessAsync(new GetAllLanguagesQuery());
_languageReadModels = languages.ToDictionary(k => GetLanguageTextKey(k.LanguageCode, k.Platform), v => v);
var groupedLanguages = languages.GroupBy(p => p.Platform)
.ToDictionary(k => k.Key.ToString().ToLower(),
v => v.ToFrozenDictionary(x => GetLanguageTextKey(x.LanguageCode, x.Platform)));
_languageReadModels = groupedLanguages.ToFrozenDictionary();
logger.LogInformation("Loading all languages completed, count: {Count}", languages.Count);
}

Expand All @@ -44,7 +49,7 @@ public async Task LoadAllLanguageTextAsync()
var languageTexts = await queryProcessor.ProcessAsync(new GetAllLanguageTextsQuery());
_languageTexts = languageTexts.GroupBy(p => new { p.LanguageCode, p.Platform },
v => new LanguageTextItem(v.Key, v.Value, v.LanguageVersion))
.ToDictionary(k => GetLanguageTextKey(k.Key.LanguageCode, k.Key.Platform),
.ToFrozenDictionary(k => GetLanguageTextKey(k.Key.LanguageCode, k.Key.Platform),
v => v.ToDictionary(k1 => k1.Key, v1 => v1));
sw.Stop();
logger.LogInformation("Loading all language texts completed, count: {Count}, timespan: {TimeSpan}", languageTexts.Count, sw.Elapsed);
Expand All @@ -68,14 +73,19 @@ private string GetLanguageTextKey(string languageCode, DeviceType deviceType)
return GetLanguageTextKey(languageCode, langPack);
}

public async Task<IReadOnlyCollection<ILanguageReadModel>> GetAllLanguagesAsync()
public async Task<IReadOnlyCollection<ILanguageReadModel>> GetAllLanguagesAsync(string languagePack)
{
if (!_languageReadModels.Any())
{
await LoadAllLanguagesAsync();
}

return _languageReadModels.Values;
if (_languageReadModels.TryGetValue(languagePack, out var languages))
{
return languages.Values;
}

return [];
}

public async Task<IReadOnlyCollection<LanguageTextItem>> GetLanguageTextAsync(string languageCode,
Expand Down Expand Up @@ -143,19 +153,24 @@ public Task<List<LanguageTextItem>> GetLanguageTextsAsync(string languageCode, s

public Task<ILanguageReadModel?> GetLanguageAsync(string languageCode, string languagePack)
{
_languageReadModels.TryGetValue(GetLanguageTextKey(languageCode, languagePack), out var languageReadModel);
ILanguageReadModel? languageReadModel = null;
if (_languageReadModels.TryGetValue(languagePack, out var languages))
{
var key = GetLanguageTextKey(languageCode, languagePack);
languages.TryGetValue(key, out languageReadModel);
}

return Task.FromResult(languageReadModel);
}

public async Task<List<LanguageTextItem>> GetLanguageDifferenceAsync(string languageCode, string languagePack, int fromVersion)
public Task<List<LanguageTextItem>> GetLanguageDifferenceAsync(string languageCode, string languagePack, int fromVersion)
{
var languageKey = GetLanguageTextKey(languageCode, languagePack);
if (_languageTexts.TryGetValue(languageKey, out var texts))
{
return texts.Values.Where(p => p.LanguageVersion > fromVersion).ToList();
return Task.FromResult(texts.Values.Where(p => p.LanguageVersion > fromVersion).ToList());
}

return [];
return Task.FromResult<List<LanguageTextItem>>([]);
}
}

0 comments on commit 86902de

Please sign in to comment.