public Sport GetSport() { var sport = _cache.Get("LeagueOfLegends") as Sport; if (sport.LastUpdate < DateTime.UtcNow.AddSeconds(-int.Parse(Configuration.GetSection("CacheRefreshRateInSeconds").Value))) { _cache.Set("LeagueOfLegends", CacheCreator.CreateSportCacheById(1, db, Configuration)); sport = _cache.Get("LeagueOfLegends") as Sport; } return(sport); }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, IMemoryCache cache, TrackerDBContext db, IConfiguration config, IHubContext <LiveEventHub> hubContext) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Error"); app.UseHsts(); } app.UseSignalR(routes => { routes.MapHub <LiveEventHub>("/live"); }); app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseSpaStaticFiles(); app.UseMvc(routes => { routes.MapRoute( "default", "{controller}/{action=Index}/{id?}"); }); app.UseSpa(spa => { spa.Options.SourcePath = "ClientApp"; if (env.IsDevelopment()) { spa.UseReactDevelopmentServer(npmScript: "start"); } }); //app.UseCors("CorsPolicy"); cache.Set("LeagueOfLegends", CacheCreator.CreateSportCacheById(1, db, Configuration)); cache.Set("CSGO", CacheCreator.CreateSportCacheById(2, db, Configuration)); cache.Set("Dota2", CacheCreator.CreateSportCacheById(3, db, Configuration)); cache.Set("Live", new Live()); RabbitMQMessageReceiver r = new RabbitMQMessageReceiver(); LiveEventHub hub = new LiveEventHub(hubContext, cache); r.LiveEventReached += hub.SendEvent; //clean up old live events from cache LiveEvent dummy = new LiveEvent(); Task.Factory.StartNew(async delegate { while (true) { var live = cache.Get("Live") as Live; foreach (var ev in live.LiveEvents) { if (DateTime.UtcNow > ev.Value.UpdateDate.AddSeconds(180)) { live.LiveEvents.TryRemove(hub.CreateLiveGameKey(ev.Value), out dummy); hub.RemoveLiveEvent(ev.Value); } } cache.Set("Live", live); await Task.Delay(5000); } }); }