-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathStarSystemCache.cs
More file actions
68 lines (59 loc) · 2.5 KB
/
StarSystemCache.cs
File metadata and controls
68 lines (59 loc) · 2.5 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using EddiDataDefinitions;
using System.Collections.Generic;
namespace EddiDataProviderService
{
public class StarSystemCache ( int expirationSeconds )
{
private readonly SlidingExpirationCache<ulong, StarSystem> starSystemCache = new StarSystemSlidingCache( expirationSeconds );
private readonly SlidingExpirationCache<string, ulong> starSystemNameCache = new NameToAddressSlidingCache( expirationSeconds );
// Store deserialized star systems in short term memory for this amount of time.
// Storage time is reset whenever the cached value is accessed.
private class StarSystemSlidingCache ( int expirationSeconds )
: SlidingExpirationCache<ulong, StarSystem>( expirationSeconds );
private class NameToAddressSlidingCache ( int expirationSeconds )
: SlidingExpirationCache<string, ulong>( expirationSeconds );
public void AddOrUpdate ( StarSystem starSystem )
{
if ( starSystem is null ) { return; }
starSystemCache.AddOrUpdate( starSystem.systemAddress, starSystem );
starSystemNameCache.AddOrUpdate( starSystem.systemname, starSystem.systemAddress );
}
public bool TryGet ( ulong systemAddress, out StarSystem result )
{
return starSystemCache.TryGet( systemAddress, out result );
}
public bool TryGet ( string systemName, out StarSystem result )
{
result = null;
if ( !string.IsNullOrEmpty( systemName ) && starSystemNameCache.TryGet( systemName, out var systemAddress ) )
{
return TryGet( systemAddress, out result );
}
return false;
}
public List<StarSystem> GetRange ( ulong[] systemAddresses )
{
var results = new List<StarSystem>();
foreach ( var systemAddress in systemAddresses )
{
if ( TryGet( systemAddress, out var cachedStarSystem ) )
{
results.Add( cachedStarSystem );
}
}
return results;
}
public List<StarSystem> GetRange ( string[] systemNames )
{
var results = new List<StarSystem>();
foreach ( var systemName in systemNames )
{
if ( TryGet( systemName, out var cachedStarSystem ) )
{
results.Add( cachedStarSystem );
}
}
return results;
}
}
}