-
-
Notifications
You must be signed in to change notification settings - Fork 182
/
Copy pathDistributedCacheMetadataService.cs
205 lines (168 loc) · 8.03 KB
/
DistributedCacheMetadataService.cs
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
using System.Text.Json;
using Fido2NetLib.Serialization;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Internal;
using Microsoft.Extensions.Logging;
namespace Fido2NetLib;
public class DistributedCacheMetadataService : IMetadataService
{
protected readonly IDistributedCache _distributedCache;
protected readonly IMemoryCache _memoryCache;
protected readonly ISystemClock _systemClock;
protected readonly List<IMetadataRepository> _repositories;
protected readonly ILogger<DistributedCacheMetadataService> _logger;
protected readonly TimeSpan _defaultMemoryCacheInterval = TimeSpan.FromHours(1);
protected readonly TimeSpan _nextUpdateBufferPeriod = TimeSpan.FromHours(25);
protected readonly TimeSpan _defaultDistributedCacheInterval = TimeSpan.FromDays(8);
protected const string CACHE_PREFIX = nameof(DistributedCacheMetadataService) + ":V2";
public DistributedCacheMetadataService(
IEnumerable<IMetadataRepository> repositories,
IDistributedCache distributedCache,
IMemoryCache memoryCache,
ILogger<DistributedCacheMetadataService> logger,
ISystemClock systemClock)
{
ArgumentNullException.ThrowIfNull(repositories);
_repositories = repositories.ToList();
_distributedCache = distributedCache;
_memoryCache = memoryCache;
_logger = logger;
_systemClock = systemClock;
}
public virtual bool ConformanceTesting()
{
return _repositories.Any(o => o.GetType() == typeof(ConformanceMetadataRepository));
}
protected virtual string GetBlobCacheKey(IMetadataRepository repository)
{
return $"{CACHE_PREFIX}:{repository.GetType().Name}:TOC";
}
protected virtual DateTimeOffset? GetNextUpdateTimeFromPayload(MetadataBLOBPayload blob)
{
if (!string.IsNullOrWhiteSpace(blob?.NextUpdate)
&& DateTimeOffset.TryParseExact(
blob.NextUpdate,
new[] { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "o" }, // Should be ISO8601 date but allow for other ISO-like formats too
System.Globalization.CultureInfo.InvariantCulture,
System.Globalization.DateTimeStyles.AssumeUniversal | System.Globalization.DateTimeStyles.AdjustToUniversal,
out var parsedDate))
{
return parsedDate;
}
return null;
}
protected virtual DateTimeOffset GetMemoryCacheAbsoluteExpiryTime(DateTimeOffset? nextUpdateTime)
{
var expiryTime = _systemClock.UtcNow.GetNextIncrement(_defaultMemoryCacheInterval);
//Ensure that memory cache expiry time never exceeds the next update time from the service
if (nextUpdateTime.HasValue && expiryTime > nextUpdateTime.Value)
expiryTime = nextUpdateTime.Value;
return expiryTime;
}
protected virtual DateTimeOffset GetDistributedCacheAbsoluteExpiryTime(DateTimeOffset? nextUpdatTime)
{
if (nextUpdatTime.HasValue)
{
if (nextUpdatTime > _systemClock.UtcNow)
return nextUpdatTime.Value.Add(_defaultDistributedCacheInterval);
}
return _systemClock.UtcNow.Add(_defaultDistributedCacheInterval);
}
protected virtual async Task<MetadataBLOBPayload> GetRepositoryPayloadWithErrorHandling(IMetadataRepository repository, CancellationToken cancellationToken = default)
{
try
{
return await repository.GetBLOBAsync(cancellationToken);
}
catch (Exception ex)
{
_logger.LogError(ex, "Could not fetch metadata from {0}", repository.GetType().Name);
return null;
}
}
protected virtual async Task StoreDistributedCachedBlob(IMetadataRepository repository, MetadataBLOBPayload payload, CancellationToken cancellationToken = default)
{
await _distributedCache.SetStringAsync(
GetBlobCacheKey(repository),
JsonSerializer.Serialize(payload, FidoModelSerializerContext.Default.MetadataBLOBPayload),
new DistributedCacheEntryOptions()
{
AbsoluteExpiration = GetDistributedCacheAbsoluteExpiryTime(GetNextUpdateTimeFromPayload(payload))
},
cancellationToken);
}
protected virtual async Task<MetadataBLOBPayload> GetDistributedCachedBlob(IMetadataRepository repository, CancellationToken cancellationToken = default)
{
var cacheKey = GetBlobCacheKey(repository);
var distributedCacheEntry = await _distributedCache.GetStringAsync(cacheKey, cancellationToken);
if (distributedCacheEntry != null)
{
try
{
var cachedBlob = JsonSerializer.Deserialize(distributedCacheEntry, FidoModelSerializerContext.Default.MetadataBLOBPayload);
var nextUpdateTime = GetNextUpdateTimeFromPayload(cachedBlob);
//If the cache until time is in the past then update and return new data, otherwise return the cached value
if (nextUpdateTime == null || nextUpdateTime.Value.Add(_nextUpdateBufferPeriod) < _systemClock.UtcNow)
{
var payload = await GetRepositoryPayloadWithErrorHandling(repository, cancellationToken);
if (payload != null)
{
await StoreDistributedCachedBlob(repository, payload, cancellationToken);
return payload;
}
}
return cachedBlob;
}
catch (JsonException ex)
{
_logger.LogWarning(ex, "{0}: Invalid BLOB value in distributed cache", nameof(DistributedCacheMetadataService));
}
}
var repoBlob = await GetRepositoryPayloadWithErrorHandling(repository, cancellationToken);
if (repoBlob != null)
{
await StoreDistributedCachedBlob(repository, repoBlob, cancellationToken);
}
return repoBlob;
}
protected virtual async Task<MetadataBLOBPayload> GetMemoryCachedPayload(IMetadataRepository repository, CancellationToken cancellationToken = default)
{
var cacheKey = GetBlobCacheKey(repository);
var memCacheEntry = await _memoryCache.GetOrCreateAsync<MetadataBLOBPayload>(cacheKey, async memCacheEntry =>
{
var distributedCacheBlob = await GetDistributedCachedBlob(repository, cancellationToken);
if (distributedCacheBlob != null)
{
var nextUpdateTime = GetNextUpdateTimeFromPayload(distributedCacheBlob);
memCacheEntry.AbsoluteExpiration = GetMemoryCacheAbsoluteExpiryTime(nextUpdateTime);
return distributedCacheBlob;
}
return null;
});
return memCacheEntry;
}
public async Task<MetadataBLOBPayloadEntry> GetEntryAsync(Guid aaguid, CancellationToken cancellationToken = default)
{
var memCacheEntry = await _memoryCache.GetOrCreateAsync<MetadataBLOBPayloadEntry>(
$"{CACHE_PREFIX}:{aaguid}",
async entry =>
{
foreach (var repo in _repositories)
{
var cachedPayload = await GetMemoryCachedPayload(repo, cancellationToken);
if (cachedPayload != null)
{
var matchingEntry = cachedPayload.Entries?.FirstOrDefault(o => o.AaGuid == aaguid);
if (matchingEntry != null)
{
entry.AbsoluteExpiration = GetMemoryCacheAbsoluteExpiryTime(GetNextUpdateTimeFromPayload(cachedPayload));
return matchingEntry;
}
}
}
return null;
});
return memCacheEntry;
}
}