ProducaoRegisto cachedRecord;
// Look for cache key.
if (!_cache.TryGetValue(CustomCacheEntries.RecordStatus, out cachedRecord))
{
// Key not in cache, so get data.
cachedRecord = _producaoRegistoService.GetActivityStatusById(registoId);
// Set cache options.
var cacheEntryOptions = new MemoryCacheEntryOptions()
// Keep in cache for this time, reset time if accessed.
.SetSlidingExpiration(TimeSpan.FromSeconds(3));
// Save data in cache.
_cache.Set(cachedRecord, CustomCacheEntries.RecordStatus);
}
I would like to invalidate this cached entry at 3 specific times.
6:58 14:58 22:58,
or basically every 8hours starting at 6:58
Is this possible?
I guess this is what you need
static TimeSpan HM(int hour, int minutes) => TimeSpan.FromHours(hour) + TimeSpan.FromMinutes(minutes);
static readonly TimeSpan[] spans = new TimeSpan[] { HM(6,58), HM(14,58), HM(22,58) };
static DateTimeOffset GetNext(DateTimeOffset now, IReadOnlyList<TimeSpan> sortedTimes)
{
var timeNow = now.TimeOfDay;
var next = sortedTimes.Where(s => timeNow < s).FirstOrDefault();
if (next == default(TimeSpan)) next = sortedTimes[0] + TimeSpan.FromDays(1);
return now.Date + next;
}
To find the entry expiration date, call function GetNext
with current time and spans
.
var next = GetNext(DateTimeOffset.Now, spans);
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments