-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseCacheService.utils.cs
More file actions
66 lines (58 loc) · 2.18 KB
/
Copy pathBaseCacheService.utils.cs
File metadata and controls
66 lines (58 loc) · 2.18 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
using Microsoft.Extensions.Caching.Hybrid;
namespace CLOOPS.microservices;
public abstract partial class BaseCacheService<TValue>
{
/// <summary>
/// Gets a cache value and throws <see cref="KeyNotFoundException"/> if the source of truth
/// reports the entry as missing (i.e. <c>HydrateSingleAsync</c> returned <c>null</c>/<c>default</c>).
/// Use this when absence is genuinely an error condition for the caller; prefer the plain
/// <see cref="GetAsync(string, System.Threading.CancellationToken)"/> for read paths where
/// "not found" is expected.
/// </summary>
public async Task<TValue> GetRequiredAsync(string key, CancellationToken ct = default)
{
var value = await GetAsync(key, ct);
if (value is null)
{
throw new KeyNotFoundException(
$"Cache key '{key}' not found in '{CacheName}' (typeof({typeof(TValue).Name})).");
}
return value;
}
private HybridCacheEntryOptions ResolveEntryOptions(CacheGetOptions? options)
{
if (options == null || (options.L1Ttl == null && options.L2Ttl == null))
{
return defaultEntryOptions;
}
return new HybridCacheEntryOptions
{
Expiration = options.L2Ttl ?? config.L2Ttl,
LocalCacheExpiration = options.L1Ttl ?? config.L1Ttl,
Flags = defaultEntryOptions.Flags,
};
}
private string GetCacheKey(string key)
{
return $"{CacheName}:{key}";
}
}
/// <summary>
/// Extension methods for <see cref="BaseCacheService{TValue}"/>.
/// </summary>
public static class BaseCacheServiceExtensions
{
/// <summary>
/// Gets a cache value and throws <see cref="KeyNotFoundException"/> if the source of truth
/// reports the entry as missing (i.e. <c>HydrateSingleAsync</c> returned <c>null</c>/<c>default</c>).
/// </summary>
public static Task<TValue> GetRequiredAsync<TValue>(
this BaseCacheService<TValue> cacheService,
string key,
CancellationToken ct = default)
where TValue : notnull
{
ArgumentNullException.ThrowIfNull(cacheService);
return cacheService.GetRequiredAsync(key, ct);
}
}