aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJP Dillingham <jpdillingham@gmail.com>2022-06-25 19:10:55 -0500
committerJP Dillingham <jpdillingham@gmail.com>2022-06-25 19:10:55 -0500
commitb1388b97f3ce4d7778103c7cae038978c3fa1284 (patch)
treebb9f6f395bee45f7a3f49defac9b0c9d92366d28 /src
parenta8deb4361362ed01c930447dcfa92a9b756d03c0 (diff)
downloadslskd-b1388b97f3ce4d7778103c7cae038978c3fa1284.tar.gz
stash statstics in state, use cache as a flag
Diffstat (limited to 'src')
-rw-r--r--src/slskd/Application.cs30
1 files changed, 15 insertions, 15 deletions
diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs
index fc064df3..9a9bf1f8 100644
--- a/src/slskd/Application.cs
+++ b/src/slskd/Application.cs
@@ -71,11 +71,6 @@ namespace slskd
private static readonly int ReconnectMaxDelayMilliseconds = 300000; // 5 minutes
- private class CacheKeys
- {
- public static readonly string AverageUploadSpeed = nameof(CacheKeys.AverageUploadSpeed);
- }
-
public Application(
OptionsAtStartup optionsAtStartup,
IOptionsMonitor<Options> optionsMonitor,
@@ -449,17 +444,22 @@ namespace slskd
}
}
- private async Task<int> GetAverageUploadSpeedFromServer()
+ private async Task RefreshUserStatisticsIfNeeded(bool force = false)
{
- if (Cache.TryGetValue(CacheKeys.AverageUploadSpeed, out int averageUploadSpeed))
+ if (force || !Cache.TryGetValue(CacheKeys.UserStatisticsToken, out _))
{
- return averageUploadSpeed;
- }
+ var stats = await Client.GetUserStatisticsAsync(OptionsAtStartup.Soulseek.Username);
- var stats = await Client.GetUserStatisticsAsync(OptionsAtStartup.Soulseek.Username);
- Cache.Set(CacheKeys.AverageUploadSpeed, stats.AverageSpeed, TimeSpan.FromHours(12));
+ State.SetValue(state => state with
+ {
+ Server = state.Server with
+ {
+ Statistics = stats,
+ },
+ });
- return stats.AverageSpeed;
+ Cache.Set(CacheKeys.UserStatisticsToken, true, TimeSpan.FromHours(4));
+ }
}
private void Client_LoggedIn(object sender, EventArgs e)
@@ -471,7 +471,7 @@ namespace slskd
_ = Client.SetSharedCountsAsync(State.CurrentValue.Shares.Directories, State.CurrentValue.Shares.Files);
// fetch our average upload speed from the server, so we can provide it along with search results
- _ = GetAverageUploadSpeedFromServer();
+ _ = RefreshUserStatisticsIfNeeded(force: true);
}
private void Client_PrivateMessageRecieved(object sender, PrivateMessageReceivedEventArgs args)
@@ -859,13 +859,13 @@ namespace slskd
if (results.Any())
{
Console.WriteLine($"[SENDING SEARCH RESULTS]: {results.Count()} records to {username} for query {query.SearchText}");
- var averageUploadSpeed = await GetAverageUploadSpeedFromServer();
+ await RefreshUserStatisticsIfNeeded();
return new SearchResponse(
Client.Username,
token,
freeUploadSlots: 1,
- uploadSpeed: averageUploadSpeed,
+ uploadSpeed: State.CurrentValue.Server.Statistics.AverageSpeed,
queueLength: 0,
fileList: results);
}