Beispiel #1
0
        public async Task Invoke(HttpContext context)
        {
            await _next(context); // Pass the control to the next middleware

            IList <string> news;

            if (!_cache.TryGetValue <IList <string> >("latestNews", out news))
            {
                news = _newsServices.GetLatestNews();
                var options = new MemoryCacheEntryOptions()
                {
                    SlidingExpiration  = TimeSpan.FromMinutes(5),
                    AbsoluteExpiration = DateTimeOffset.UtcNow.AddHours(1)
                };

                // The entry can be removed, is not priority
                options.Priority = CacheItemPriority.Low;

                // Never removed the entry automatically
                // We do this manually
                options.Priority = CacheItemPriority.NeverRemove;

                _cache.Set("latestNews", news, options);

                // To invalidate an entry
                _cache.Remove("latestNews");
            }

            await context.Response.WriteAsync($"\nNumber of News: {news.Count}");
        }
        public HomeEntity Get()
        {
            HomeEntity entity = new HomeEntity();

            entity.News        = newsServices.GetLatestNews();
            entity.Events      = eventServices.GetAllEvents();
            entity.Departments = deptServices.GetAllDepartments();
            return(entity);
        }
 public IEnumerable <NewsEntity> GetLatestNews()
 {
     return(newsServices.GetLatestNews());
 }