private static void SaveToCache(BestStoryInfo bestStoryInfo) { try { var objectAsString = JsonConvert.SerializeObject(bestStoryInfo); _cacheDB?.StringSet(bestStoryInfo.Id.ToString(), objectAsString); } catch (Exception ex) { Log.Error($"Error attempting to save a story to the cache for story {bestStoryInfo.Id}", ex); } }
/// <summary> /// This pulls the data for the passed ID. It intentionally ignores if there was an error attempting to pull from or write to cache /// Also if there was a problem attempting to pull the item from the actual source, this error is ignored as well as TBD the reason, so a stub /// object will be created in its place /// </summary> /// <param name="id"></param> /// <returns></returns> public static async Task <BestStoryInfo> GetBestStoryInfo(int id) { try { var result = GetFromCache(id); if (result != null) { return(result); } var dataApi = new HackerNewsAPI.HackerNewsDataAPI(new Uri(HackerNewsDataAPI)); var item = await dataApi.GetItemAsync(id); var bestStoryInfo = new BestStoryInfo { Id = item.ID.GetValueOrDefault(0), By = item.By, Title = item.Title }; SaveToCache(bestStoryInfo); return(bestStoryInfo); } catch (Exception ex) { Log.Error($"Error attempting to read information from Hacker News for the item {id}", ex); var bestStoryInfo = new BestStoryInfo { Id = id, By = "<unknown>", Title = "<Problem encountered attempting to read informtion from Hacker News>" }; return(bestStoryInfo); } }