public async Task TidyCacheAsync(CancellationToken cancellationToken) { var lastCleared = await _stateRepository.GetLastStagingDateClearedAsync(cancellationToken); var retentionDate = DateTime.Today.AddDays(-_configuration.NumberOfDaysToRetainStagingData); _logger.Info($"Tidying cache staging data between {lastCleared} and {retentionDate}"); try { while (lastCleared < retentionDate) { lastCleared = lastCleared.AddDays(1); var numberOfRowsDeleted = await _providerRepository.ClearStagingDataForDateAsync(lastCleared, cancellationToken); await _stateRepository.SetLastStagingDateClearedAsync(lastCleared, cancellationToken); _logger.Info($"Cleared {numberOfRowsDeleted} rows for {lastCleared}"); } } catch (Exception ex) { throw new Exception($"Errored when clearing data for {lastCleared} - {ex.Message}", ex); } _logger.Info($"Finished tidying cache staging data upto {lastCleared}"); }