Added caching to the cultures
This commit is contained in:
parent
f27366e0a2
commit
276fa51e92
@ -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>();
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user