Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions LocalLLMServerManager.Tests/AppSettingsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,14 @@ public void SettingsService_SaveAndLoad_RoundTripsSuccessfully()
service.SaveSettings(original);
}
}

[Fact]
public void SettingsService_UsesCachedInstance_AfterInitialLoad()
{
var service = new SettingsService();
var firstLoad = service.LoadSettings();
var secondLoad = service.LoadSettings();

Assert.Same(firstLoad, secondLoad);
}
}
16 changes: 14 additions & 2 deletions Services/SettingsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace LocalLLMServerManager.Services;
public class SettingsService : ISettingsService
{
private static readonly object SettingsLock = new();
private static AppSettings? _cachedSettings;

public string SettingsFilePath()
{
Expand All @@ -16,6 +17,11 @@ public AppSettings LoadSettings()
{
lock (SettingsLock)
{
if (_cachedSettings != null)
{
return _cachedSettings;
}

for (int i = 0; i < 5; i++)
{
try
Expand All @@ -24,7 +30,8 @@ public AppSettings LoadSettings()
if (File.Exists(path))
{
var json = File.ReadAllText(path);
return JsonSerializer.Deserialize<AppSettings>(json) ?? new AppSettings();
_cachedSettings = JsonSerializer.Deserialize<AppSettings>(json) ?? new AppSettings();
return _cachedSettings;
}
break;
}
Expand All @@ -33,7 +40,9 @@ public AppSettings LoadSettings()
Thread.Sleep(50);
}
}
return new AppSettings();

_cachedSettings = new AppSettings();
return _cachedSettings;
}
}

Expand All @@ -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
Expand Down
Loading