public async Task <IActionResult> GetChapterStatistics(int courseId, int chapterNumber, [FromQuery] DateTime?date) { if (courseId < 1 || chapterNumber < 1) { return(BadRequest()); } var dateUtc = date?.ToUniversalTime(); bool useCache = !(dateUtc.HasValue && DateTime.UtcNow.Subtract(dateUtc.Value).TotalSeconds > CacheTimeInSeconds); var cacheKey = $"GetChapterStatistics-{courseId}-{chapterNumber}"; ChapterStatisticsModel model; if (!useCache || !_memoryCache.TryGetValue(cacheKey, out model)) { try { var chapter = await _chapterService.LoadChapterAsync(courseId, chapterNumber); var chapterStatistics = await _chapterService.GetChapterStatisticsAsync(chapter.Id, dateUtc); model = _chapterConverter.ToChapterStatisticsModel(chapter, chapterStatistics); if (useCache) { _memoryCache.Set(cacheKey, model, DateTime.Now.AddSeconds(CacheTimeInSeconds)); } } catch (DataNotFoundException) { return(NotFound()); } } return(Ok(model)); }