/// <summary> /// Make a request to the OpenWeatherAPI to get the three hour forecast /// for the location of the ID specified /// </summary> /// <param name="id">OpenWeatherAPI definition for the ID specified</param> /// <param name="redis">The Redis connection to use</param> /// <returns>Three Hour Weather object as JSON</returns> public static async Task <string> Forecast(int id, Redis_Connection redis) { // build the key string key = string.Format("forecast_{0}", id); // check to see if we have the current weather for this location // within our Redis cache string response = Cache.Check(key, redis); // if we have it, return it // otherwise, make the request to the API if (!string.IsNullOrWhiteSpace(response)) { return(response); } // we need to make the request to the OpenWeatherAPI to get the data response = await rest.MakeRequest("GET", string.Format("forecast?id={0}", id)); // spin off a task to update the databases while // not locking up ui for the end user // 1. add the response to Redis with 5 minute expiration // 2. add/update SQL to hold the location Cache.UpdateDBAndCache(key, response, redis); // return the response return(response); }
public void SetUp() { // instantiate redis redis = new Redis_Connection(); // create a random and non-repeatable key for this test test_key = Guid.NewGuid().ToString("N"); }
/// <summary> /// Fire and forget function that will update our data sources /// without blocking UI for enduser /// </summary> /// <param name="key">the key to update in Redis</param> /// <param name="JSON">the JSON to add to Redis</param> /// <param name="redis">the redis connection to use</param> public static void UpdateDBAndCache(string key, string JSON, Redis_Connection redis) { try { // spin off a non-blocking task Task.Run(() => { // update Redis redis.set(key, JSON); }).ConfigureAwait(false); } catch (Exception e) { } }
/// <summary> /// Make a request to get both the current weather and the forecasted weather /// for the location specified /// /// This will make the request for each asyncrhonously and perform the action /// of building the appropriate response object in JSON /// </summary> /// <param name="id">the ID of the location to get the weather from</param> /// <param name="redis">The Redis connection to use</param> /// <returns>{ current: {current}, forecast: {forecast} }</returns> public static async Task <Current_and_Forecasted_Weather> CurrentAndForecast(int id, Redis_Connection redis) { // start the tasks of retrieving the information // from either the cache or the API Task <string> currentWeatherTask = Current(id, redis); Task <string> forecastedWeatherTask = Forecast(id, redis); // wait until both tasks are completed await Task.WhenAll(currentWeatherTask, forecastedWeatherTask); // create the object and return it return(new Current_and_Forecasted_Weather(currentWeatherTask.Result, forecastedWeatherTask.Result)); }
public void SetUp() { redis = new Redis_Connection(); }
/// <summary> /// internal function to check to see if we need to make the request /// to the OpenWeatherAPI. If we have the request cached in Redis, then /// there is no reason to make this request /// </summary> /// <param name="key">the Redis Cache key to check</param> /// <param name="redis">the connection to the redis server to use</param> /// <returns>string, JSON representation of the object if it exists. Null otherwise</returns> public static string Check(string key, Redis_Connection redis) { // check the redis cache for the value we need return(redis.get(key)); }