From 2442ae69affc69d148f9953b6be62719ec7358f8 Mon Sep 17 00:00:00 2001 From: spelech <28486500+spelech@users.noreply.github.com> Date: Sun, 20 Sep 2026 15:22:14 +0000 Subject: [PATCH] perf: optimize SettingsService with static in-memory cache Implemented a thread-safe, static in-memory cache inside `SettingsService.cs`. The cache is lazy-loaded upon the first call to `LoadSettings()` and updated during `SaveSettings()`. This prevents blocking I/O calls for repeated file reads while maintaining existing `ISettingsService` behavior and testing capabilities. Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> --- Services/SettingsService.cs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) 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