Added caching to the cultures

This commit is contained in:
Thorsten Sommer 2022-07-30 23:14:39 +02:00
parent f27366e0a2
commit 276fa51e92
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108

View File

@ -232,20 +232,32 @@ public static class AppSettings
#region Get a culture
// Cache the cultures:
private static readonly Dictionary<int, string> CACHE_CULTURES = new();
public static async Task<string> GetCultureCode(int index)
{
// Check the cache:
if (CACHE_CULTURES.TryGetValue(index, out var cultureCode))
return cultureCode;
// Get the database:
await using var db = ProcessorMeta.ServiceProvider.GetRequiredService<DataContext>();
// Get the culture code:
var code = await db.Settings.FirstOrDefaultAsync(n => n.Code == SettingNames.CULTURE && n.IntegerValue == index);
// When the culture code is not set, fake the default culture as en-US:
if(code == null)
// When the culture code is not set & index = 1, use the default culture en-US:
if(code is null && index == 1)
{
CACHE_CULTURES.Add(index, "en-US");
return "en-US";
}
// Return the code:
return code.TextValue;
// Update the cache & return the code:
var codeText = code?.TextValue ?? string.Empty;
CACHE_CULTURES[index] = codeText;
return codeText;
}
#endregion
@ -254,6 +266,9 @@ public static class AppSettings
public static async Task SetCultureCode(int index, string code)
{
// Update the cache:
CACHE_CULTURES[index] = code;
// Get the database:
await using var db = ProcessorMeta.ServiceProvider.GetRequiredService<DataContext>();