-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathCacheKeyFactory.cs
More file actions
58 lines (50 loc) · 1.86 KB
/
CacheKeyFactory.cs
File metadata and controls
58 lines (50 loc) · 1.86 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
using System.Text;
using ByteSync.ServerCommon.Business.Repositories;
using ByteSync.ServerCommon.Business.Settings;
using ByteSync.ServerCommon.Entities;
using ByteSync.ServerCommon.Interfaces.Factories;
using Microsoft.Extensions.Options;
namespace ByteSync.ServerCommon.Factories;
public class CacheKeyFactory : ICacheKeyFactory
{
private readonly string _prefix;
public CacheKeyFactory(IOptions<RedisSettings> redisSettings)
{
_prefix = redisSettings.Value.Prefix;
}
public CacheKey Create(EntityType entityType, string entityId)
{
string entityTypeName = GetEntityTypeName(entityType);
StringBuilder sb = new StringBuilder(_prefix);
sb.Append(':');
sb.Append(entityTypeName);
sb.Append(':');
sb.Append(entityId);
var cacheKeyValue = sb.ToString();
var cacheKey = new CacheKey
{
EntityType = entityType,
EntityId = entityId,
Value = cacheKeyValue
};
return cacheKey;
}
private string GetEntityTypeName(EntityType entityType)
{
return entityType switch
{
EntityType.Session => "Session",
EntityType.Inventory => "Inventory",
EntityType.Synchronization => "Synchronization",
EntityType.SharedFile => "SharedFile",
EntityType.SessionSharedFiles => "SessionSharedFiles",
EntityType.TrackingAction => "TrackingAction",
EntityType.Client => "Client",
EntityType.ClientSoftwareVersionSettings => "ClientSoftwareVersionSettings",
EntityType.CloudSessionProfile => "CloudSessionProfile",
EntityType.Lobby => "Lobby",
EntityType.MessageDefinition => "MessageDefinition",
_ => throw new ArgumentOutOfRangeException(nameof(entityType), entityType, null)
};
}
}