aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2022-09-07 23:29:07 -0500
committerJP Dillingham <jp@dillingham.me>2022-09-07 23:29:07 -0500
commit2729bc7c6315bf1555a8761de94134e5171d4014 (patch)
treeb5215a46e454f4e78def73ac39049c1ada509272 /src
parent425991e9fcb3519eaff0a0411e0777e6e74c1503 (diff)
downloadslskd-2729bc7c6315bf1555a8761de94134e5171d4014.tar.gz
load cache on start, or force scan. cache first browse response
Diffstat (limited to 'src')
-rw-r--r--src/slskd/Application.cs60
1 files changed, 37 insertions, 23 deletions
diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs
index 6c6ee475..07cf5dbd 100644
--- a/src/slskd/Application.cs
+++ b/src/slskd/Application.cs
@@ -369,22 +369,23 @@ namespace slskd
_ = CheckVersionAsync();
}
- //if (OptionsAtStartup.Flags.NoShareScan)
- //{
- // Log.Warning("Not scanning shares; 'no-share-scan' option is enabled. Search and browse results will remain disabled until a manual scan is completed.");
- //}
- //else
- //{
- // _ = Shares.StartScanAsync();
- //}
-
- Shares.LoadFromDisk();
- Log.Information("Shares loaded from disk");
-
- Log.Information("Caching browse response...");
- var browse = new BrowseResponse(await Shares.BrowseAsync()).ToByteArray();
- System.IO.File.WriteAllBytes(Path.Combine(Program.DataDirectory, "browse.cache"), browse);
- Log.Information("Browse response cached.");
+ if (await Shares.TryLoadFromDiskAsync())
+ {
+ Log.Information("Loaded share cache from disk");
+ }
+ else
+ {
+ Log.Warning("Failed to load share cache from disk. A share scan is required.");
+
+ if (OptionsAtStartup.Flags.NoShareScan)
+ {
+ Log.Warning("Not scanning shares; 'no-share-scan' option is enabled. Search and browse results will remain disabled until a manual scan is completed.");
+ }
+ else
+ {
+ _ = Shares.StartScanAsync();
+ }
+ }
if (OptionsAtStartup.Flags.NoConnect)
{
@@ -427,20 +428,33 @@ namespace slskd
BrowseResponse response = default;
- var cacheFilename = Path.Combine(Program.DataDirectory, "browse.cache");
+ var cacheFilename = Path.Combine(Program.DataDirectory, Filenames.BrowseCache);
var cacheFileInfo = new System.IO.FileInfo(cacheFilename);
- if (cacheFileInfo.Exists)
- {
- var stream = new System.IO.FileStream(cacheFilename, FileMode.Open, FileAccess.Read);
- response = new RawBrowseResponse(cacheFileInfo.Length, stream);
- }
- else
+ if (!cacheFileInfo.Exists)
{
+ Log.Warning("Browse response not cached. Rebuilding...");
+
var directories = await Shares.BrowseAsync();
response = new BrowseResponse(directories);
+
+ Log.Information("Caching browse response...");
+ System.IO.File.WriteAllBytes(cacheFilename, response.ToByteArray());
+ Log.Information("Browse response cached successfully");
}
+ cacheFileInfo = new System.IO.FileInfo(cacheFilename);
+
+ if (!cacheFileInfo.Exists)
+ {
+ throw new IOException("Unable to access recently written browse cache; is the file still open?");
+ }
+
+ var stream = new System.IO.FileStream(cacheFilename, FileMode.Open, FileAccess.Read);
+ response = new RawBrowseResponse(cacheFileInfo.Length, stream);
+
+ Log.Information("Returned cached browse response for {User}", username);
+
sw.Stop();
Metrics.Browse.ResponseLatency.Observe(sw.ElapsedMilliseconds);