diff --git a/Services/SettingsService.cs b/Services/SettingsService.cs index 6c03d1e..24cbd4f 100644 --- a/Services/SettingsService.cs +++ b/Services/SettingsService.cs @@ -6,6 +6,7 @@ namespace LocalLLMServerManager.Services; public class SettingsService : ISettingsService { private static readonly object SettingsLock = new(); + private static AppSettings? _cachedSettings; public string SettingsFilePath() { @@ -16,6 +17,11 @@ public AppSettings LoadSettings() { lock (SettingsLock) { + if (_cachedSettings != null) + { + return _cachedSettings; + } + for (int i = 0; i < 5; i++) { try @@ -24,7 +30,8 @@ public AppSettings LoadSettings() if (File.Exists(path)) { var json = File.ReadAllText(path); - return JsonSerializer.Deserialize(json) ?? new AppSettings(); + _cachedSettings = JsonSerializer.Deserialize(json) ?? new AppSettings(); + return _cachedSettings; } break; } @@ -33,7 +40,9 @@ public AppSettings LoadSettings() Thread.Sleep(50); } } - return new AppSettings(); + + _cachedSettings = new AppSettings(); + return _cachedSettings; } } @@ -48,6 +57,9 @@ public void SaveSettings(AppSettings settings) var path = SettingsFilePath(); var json = JsonSerializer.Serialize(settings, new JsonSerializerOptions { WriteIndented = true }); File.WriteAllText(path, json); + + // Update cache after successful save + _cachedSettings = settings; break; } catch