public string UpdateData() { var timer = new Stopwatch(); timer.Start(); var client = new RestClient("https://workstatus.poyi.tk/api/v1/data?cache=bypass") { Timeout = -1 }; var request = new RestRequest(Method.GET); IRestResponse response = client.Execute(request); StatusModel statusModel = Newtonsoft.Json.JsonConvert.DeserializeObject <StatusModel>(response.Content); _cache.Set("Cache", statusModel); List <OWMModel> oWMModels = new List <OWMModel>(); foreach (var item in CitiesEn) { client = new RestClient("https://api.openweathermap.org/data/2.5/weather?q=" + item + ",TW&units=metric&appid=44d31a1e35fb9166af8c0af891f9cf10&lang=zh_tw") { Timeout = -1 }; request = new RestRequest(Method.GET); request.AddHeader("Accept", "*/*"); request.AddHeader("User-Agent", "Mozilla/5.0 (compatible; PoyiCorporationBot/2.1; +http://www.poyi.tk/bot.html)"); request.AddHeader("Accept-Encoding", "gzip, deflate, br"); request.AddHeader("Connection", "keep-alive"); request.AddHeader("Cache-Control", "no-cache"); response = client.Execute(request); OWMModel oWMModel = Newtonsoft.Json.JsonConvert.DeserializeObject <OWMModel>(response.Content); oWMModels.Add(oWMModel); } _cache.Set("WeatherCache", oWMModels); timer.Stop(); TimeSpan timeTaken = timer.Elapsed; return("Time taken: " + timeTaken.ToString(@"m\:ss\.fff")); }
public IActionResult Detail(string id) { if (id != null) { RestClient client = new RestClient("http://api.openweathermap.org/data/2.5/weather?q=" + CitiesCh[Convert.ToInt16(id)] + "&appid=44d31a1e35fb9166af8c0af891f9cf10&lang=zh_tw") { Timeout = -1 }; var request = new RestRequest(Method.GET); request.AddHeader("Accept", "*/*"); request.AddHeader("User-Agent", "Mozilla/5.0 (compatible; PoyiCorporationBot/2.1; +http://www.poyi.tk/bot.html)"); request.AddHeader("Accept-Encoding", "gzip, deflate, br"); request.AddHeader("Connection", "keep-alive"); request.AddHeader("Cache-Control", "no-cache"); IRestResponse response = client.Execute(request); OWMModel oWMModel = Newtonsoft.Json.JsonConvert.DeserializeObject <OWMModel>(response.Content); ViewBag.OWM = oWMModel; if (_cache.TryGetValue("Cache", out StatusModel statusModel)) { ViewBag.StatusModel = statusModel; } else { UpdateData(); if (_cache.TryGetValue("Cache", out statusModel)) { ViewBag.StatusModel = statusModel; } else { UpdateData(); } } } return(View()); }