Declare um semáforo
static SemaphoreSlim _semaphoreSlim = new SemaphoreSlim(1, 1);
Busque a informação no cache
var users = _memoryCache.Get<IEnumerable<UserModel>>("_Users");
Se não achar no cache, consuma o serviço e armazene no cache
if (users == null)
{
await _semaphoreSlim.WaitAsync();
var response = await _httpClient.GetAsync($"/api/v2/user/");
var stringResponse = await response.Content.ReadAsStringAsync();
if (response.IsSuccessStatusCode)
{
users = JsonConvert.DeserializeObject<IEnumerable<UserModel>>(stringResponse);
var cacheOptions = new MemoryCacheEntryOptions()
.SetSlidingExpiration(TimeSpan.FromHours(12));
_memoryCache.Set("_Users", users, cacheOptions);
}
_semaphoreSlim.Release();
}
Para reload basta refazer o cache
var users = JsonConvert.DeserializeObject<IEnumerable<UserModel>>(stringResponse);
var cacheOptions = new MemoryCacheEntryOptions()
.SetSlidingExpiration(TimeSpan.FromHours(12));
_memoryCache.Set("_Users", users, cacheOptions);