Skip to content

Commit

Permalink
Merge pull request #51 from Elihpmap/ConfigCaching
Browse files Browse the repository at this point in the history
Added caching of ConfigLines to avoid rereading the whole file each time
  • Loading branch information
nref authored Nov 27, 2024
2 parents 8b39063 + feb495b commit 5bfac74
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions BrowseRouter/ConfigService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ public ConfigService()
// Fix for self-contained publishing
this.ConfigPath = Path.Combine(Path.GetDirectoryName(App.ExePath)!, "config.ini");
}

private IEnumerable<string>? _configLines = null;
private IEnumerable<string> ConfigLines => _configLines ??= ReadFile();

public bool GetIsLogEnabled() => File.Exists(ConfigPath)
&& GetConfig(ReadFile(), "log")
&& GetConfig("log")
.Select(SplitConfig)
.Any(kvp => kvp.Key == "enabled" && kvp.Value == "true");

Expand All @@ -31,7 +34,7 @@ public bool GetIsLogEnabled() => File.Exists(ConfigPath)

private NotifyPreference GetNotifyPreferenceCore()
{
var notifyConfig = GetConfig(ReadFile(), "notify")
var notifyConfig = GetConfig("notify")
.Select(SplitConfig)
.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);

Expand All @@ -53,7 +56,7 @@ private NotifyPreference GetNotifyPreferenceCore()

private LogPreference GetLogPreferenceCore()
{
var logConfig = GetConfig(ReadFile(), "log")
var logConfig = GetConfig("log")
.Select(SplitConfig)
.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);

Expand All @@ -69,11 +72,8 @@ public IEnumerable<UrlPreference> GetUrlPreferences(string configType)
if (!File.Exists(ConfigPath))
throw new InvalidOperationException($"The config file was not found: {ConfigPath}");

// Poor mans INI file reading... Skip comment lines (TODO: support comments on other lines).
IEnumerable<string> configLines = ReadFile();

// Read the browsers section into a dictionary.
var browsers = GetConfig(configLines, "browsers")
var browsers = GetConfig("browsers")
.Select(SplitConfig)
.Select(kvp => new Browser { Name = kvp.Key, Location = kvp.Value })
.ToDictionary(b => b.Name);
Expand All @@ -85,7 +85,7 @@ public IEnumerable<UrlPreference> GetUrlPreferences(string configType)
}

// Read the url preferences
var urls = GetConfig(configLines, configType)
var urls = GetConfig(configType)
.Select(SplitConfig)
.Select(kvp => new UrlPreference { UrlPattern = kvp.Key, Browser = browsers[kvp.Value] })
.Where(up => up.Browser != null);
Expand All @@ -98,15 +98,19 @@ public IEnumerable<UrlPreference> GetUrlPreferences(string configType)

private IEnumerable<string> ReadFile()
{
if (!File.Exists(ConfigPath))
throw new InvalidOperationException($"The config file was not found at \"{ConfigPath}\"");

// Poor mans INI file reading... Skip comment lines (TODO: support comments on other lines).
return File.ReadAllLines(ConfigPath)
.Select(l => l.Trim())
.Where(l => !string.IsNullOrWhiteSpace(l) && !l.StartsWith(";") && !l.StartsWith("#"));
.Select(l => l.Trim())
.Where(l => !string.IsNullOrWhiteSpace(l) && !l.StartsWith(';') && !l.StartsWith('#'));
}

private IEnumerable<string> GetConfig(IEnumerable<string> configLines, string configName)
private IEnumerable<string> GetConfig(string configName)
{
// Read everything from [configName] up to the next [section].
return configLines
return ConfigLines
.SkipWhile(l => !l.StartsWith($"[{configName}]", StringComparison.OrdinalIgnoreCase))
.Skip(1)
.TakeWhile(l => !l.StartsWith("[", StringComparison.OrdinalIgnoreCase))
Expand Down

0 comments on commit 5bfac74

Please sign in to comment.