From 276fa51e92627f47e4743b9d8664672f56205d4a Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Sat, 30 Jul 2022 23:14:39 +0200 Subject: [PATCH] Added caching to the cultures --- I18N Commander/Processor/AppSettings.cs | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/I18N Commander/Processor/AppSettings.cs b/I18N Commander/Processor/AppSettings.cs index a5eee75..555731d 100644 --- a/I18N Commander/Processor/AppSettings.cs +++ b/I18N Commander/Processor/AppSettings.cs @@ -232,20 +232,32 @@ public static class AppSettings #region Get a culture + // Cache the cultures: + private static readonly Dictionary CACHE_CULTURES = new(); + public static async Task 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(); // 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();