-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseMapCache.cs
More file actions
37 lines (31 loc) · 1.67 KB
/
Copy pathBaseMapCache.cs
File metadata and controls
37 lines (31 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using System.Collections.Concurrent;
using RustPlusBot.Features.Connections.Listening;
namespace RustPlusBot.Features.Map.Composing;
/// <summary>Caches the static-per-wipe base map image per (guild, server). Singleton so the cache survives across refreshes.</summary>
/// <param name="query">The live query seam used to fetch the base map on a cache miss.</param>
public sealed class BaseMapCache(IRustServerQuery query)
{
private readonly ConcurrentDictionary<(ulong Guild, Guid Server), byte[]> _images = new();
/// <summary>Gets the cached base map, fetching and caching it on a miss. Null results are not cached.</summary>
/// <param name="guildId">The owning guild snowflake.</param>
/// <param name="serverId">The target server id.</param>
/// <param name="cancellationToken">A cancellation token.</param>
/// <returns>The base-map JPEG bytes, or null if unavailable.</returns>
public async Task<byte[]?> GetAsync(ulong guildId, Guid serverId, CancellationToken cancellationToken)
{
if (_images.TryGetValue((guildId, serverId), out var cached))
{
return cached;
}
var fetched = await query.GetMapImageAsync(guildId, serverId, cancellationToken).ConfigureAwait(false);
if (fetched is not null)
{
_images[(guildId, serverId)] = fetched;
}
return fetched;
}
/// <summary>Evicts the cached base map for a server (called on disconnect).</summary>
/// <param name="guildId">The owning guild snowflake.</param>
/// <param name="serverId">The target server id.</param>
public void Clear(ulong guildId, Guid serverId) => _images.TryRemove((guildId, serverId), out _);
}