public async Task Run([TimerTrigger("%TimedCacheRefresherCronExpression%")] TimerInfo timerInfo, CancellationToken cancellationToken, ILogger log)
        {
            _logger.LogInformation($"TimedCacheRefresher CRON trigger executed at : {DateTimeOffset.Now}");

            Stopwatch stopwatch = Stopwatch.StartNew();

            try
            {
                await _memDistCache.RefreshDataAsync(async (token) => await _volunteersForCacheGetter.GetAllVolunteersAsync(token), CacheKey.AllCachedVolunteerDtos.ToString(), cancellationToken);
            }
            catch (Exception ex)
            {
                _logger.LogError("Error running TimedCacheRefresher", ex);
                throw;
            }

            stopwatch.Stop();

            _logger.LogInformation($"TimedCacheRefresher CRON trigger took: {stopwatch.Elapsed:%m' min '%s' sec'}");
        }
Пример #2
0
        /// <summary>
        /// Get volunteers using cache.
        /// </summary>
        public async Task <IEnumerable <CachedVolunteerDto> > GetCachedVolunteersAsync(VolunteerType volunteerType, CancellationToken cancellationToken)
        {
            // Don't refresh data in cache because this will be refreshed through a Timed trigger.  This is because getting dependencies on a new thread using IServiceScopeFactory throws an error in Azure Functions (Scope disposed{no name, Parent={no name}} is disposed and scoped instances are disposed and no longer available).
            IEnumerable <CachedVolunteerDto> cachedVolunteerDtos = await _memDistCache.GetCachedDataAsync(async (token) => await _volunteersForCacheGetter.GetAllVolunteersAsync(token), CacheKey.AllCachedVolunteerDtos.ToString(), RefreshBehaviour.DontRefreshData, cancellationToken);

            List <CachedVolunteerDto> matchingVolunteers = cachedVolunteerDtos.Where(x => x.VolunteerType.HasAnyFlags(volunteerType)).ToList();

            return(matchingVolunteers);
        }