-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseCacheService.validations.cs
More file actions
74 lines (63 loc) · 2.41 KB
/
Copy pathBaseCacheService.validations.cs
File metadata and controls
74 lines (63 loc) · 2.41 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
69
70
71
72
73
74
using System.Reflection;
namespace CLOOPS.microservices;
public abstract partial class BaseCacheService<TValue>
{
private static void ValidateConfig(CacheConfigAttribute config)
{
if (string.IsNullOrWhiteSpace(config.Name))
{
throw new InvalidOperationException("[CacheConfig] Name cannot be empty.");
}
if (config.Name.Contains(':', StringComparison.Ordinal))
{
throw new InvalidOperationException("[CacheConfig] Name cannot contain ':'.");
}
if (config.L1Ttl <= TimeSpan.Zero)
{
throw new InvalidOperationException("[CacheConfig] L1Ttl must be positive.");
}
if (config.L2Ttl <= TimeSpan.Zero)
{
throw new InvalidOperationException("[CacheConfig] L2Ttl must be positive.");
}
}
private void ValidateBulkHydrationRequirements()
{
if (!string.IsNullOrWhiteSpace(config.RefreshCron) && !hasBulkHydration.Value)
{
throw new InvalidOperationException(
$"{GetType().FullName} must override HydrateAllAsync when [CacheConfig.RefreshCron] is set.");
}
if (config.RefreshOnStartup && !hasBulkHydration.Value)
{
throw new InvalidOperationException(
$"{GetType().FullName} must override HydrateAllAsync when [CacheConfig.RefreshOnStartup = true].");
}
}
private void ValidateEntryKey(string key)
{
if (string.IsNullOrWhiteSpace(key))
{
throw new ArgumentException("Cache key cannot be empty.", nameof(key));
}
if (key.Contains(':', StringComparison.Ordinal))
{
throw new ArgumentException("Cache key cannot contain ':'.", nameof(key));
}
}
private void EnsureBulkHydrationSupported()
{
if (!hasBulkHydration.Value)
{
throw new InvalidOperationException(
$"{GetType().FullName} does not support bulk cache refresh. Override HydrateAllAsync to enable RefreshCron / RefreshOnStartup / explicit RefreshAllAsync calls.");
}
}
private bool ComputeHasBulkHydration()
{
var method = GetType().GetMethod(
nameof(HydrateAllAsync),
BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy);
return method != null && method.DeclaringType != typeof(BaseCacheService<TValue>);
}
}