From 90be288d176133ef90f1efaac09d2ad82478c702 Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Sat, 30 Jul 2022 17:04:32 +0200 Subject: [PATCH] Added setting cache --- I18N Commander/Processor/AppSettings.cs | 42 +++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/I18N Commander/Processor/AppSettings.cs b/I18N Commander/Processor/AppSettings.cs index 1d97a66..90f1f94 100644 --- a/I18N Commander/Processor/AppSettings.cs +++ b/I18N Commander/Processor/AppSettings.cs @@ -9,11 +9,18 @@ public static class AppSettings { #region DeepL Mode + private static SettingDeepLMode CACHE_DEEPL_MODE = SettingDeepLMode.DISABLED; + private static bool CACHE_DEEPL_MODE_IS_LOADED = false; + public static async Task SetDeepLMode(SettingDeepLMode mode) { // Convert the enum to its int value: var intValue = (int)mode; + // Update the cache: + CACHE_DEEPL_MODE = mode; + CACHE_DEEPL_MODE_IS_LOADED = true; + // Get the database: await using var db = ProcessorMeta.ServiceProvider.GetRequiredService(); @@ -40,6 +47,9 @@ public static class AppSettings public static async Task GetDeepLMode() { + if (CACHE_DEEPL_MODE_IS_LOADED) + return CACHE_DEEPL_MODE; + // Get the database: await using var db = ProcessorMeta.ServiceProvider.GetRequiredService(); @@ -57,15 +67,26 @@ public static class AppSettings await db.Settings.AddAsync(setting); await db.SaveChangesAsync(); - return (SettingDeepLMode) setting.IntegerValue; + var mode = (SettingDeepLMode) setting.IntegerValue; + CACHE_DEEPL_MODE = mode; + CACHE_DEEPL_MODE_IS_LOADED = true; + + return mode; } #endregion #region DeepL API Key + private static string CACHE_DEEPL_API_KEY = string.Empty; + private static bool CACHE_DEEPL_API_KEY_IS_LOADED = false; + public static async Task SetDeepLAPIKey(string apiKey) { + // Update the cache: + CACHE_DEEPL_API_KEY = apiKey; + CACHE_DEEPL_API_KEY_IS_LOADED = true; + // Get the database: await using var db = ProcessorMeta.ServiceProvider.GetRequiredService(); @@ -109,18 +130,29 @@ public static class AppSettings await db.Settings.AddAsync(setting); await db.SaveChangesAsync(); - return setting.TextValue; + var key = setting.TextValue; + CACHE_DEEPL_API_KEY = key; + CACHE_DEEPL_API_KEY_IS_LOADED = true; + + return key; } #endregion #region DeepL Action + private static SettingDeepLAction CACHE_DEEPL_ACTION = SettingDeepLAction.MANUAL; + private static bool CACHE_DEEPL_ACTION_IS_LOADED = false; + public static async Task SetDeepLAction(SettingDeepLAction action) { // Convert the enum to its int value: var intValue = (int)action; + // Update the cache: + CACHE_DEEPL_ACTION = action; + CACHE_DEEPL_ACTION_IS_LOADED = true; + // Get the database: await using var db = ProcessorMeta.ServiceProvider.GetRequiredService(); @@ -164,7 +196,11 @@ public static class AppSettings await db.Settings.AddAsync(setting); await db.SaveChangesAsync(); - return (SettingDeepLAction) setting.IntegerValue; + var action = (SettingDeepLAction) setting.IntegerValue; + CACHE_DEEPL_ACTION = action; + CACHE_DEEPL_ACTION_IS_LOADED = true; + + return action; } #endregion