Added setting cache

This commit is contained in:
Thorsten Sommer 2022-07-30 17:04:32 +02:00
parent c211c9ef62
commit 90be288d17
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108

View File

@ -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<DataContext>();
@ -40,6 +47,9 @@ public static class AppSettings
public static async Task<SettingDeepLMode> GetDeepLMode()
{
if (CACHE_DEEPL_MODE_IS_LOADED)
return CACHE_DEEPL_MODE;
// Get the database:
await using var db = ProcessorMeta.ServiceProvider.GetRequiredService<DataContext>();
@ -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<DataContext>();
@ -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<DataContext>();
@ -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