From 98f0bc5c2616faa56b709d7a779ce6792fe2dd09 Mon Sep 17 00:00:00 2001 From: John Lewin Date: Tue, 29 May 2018 13:11:38 -0700 Subject: [PATCH] Use custom caching pattern for ExplorePanel - Issue MatterHackers/MCCentral#3382 Improve caching/loading of start page --- ApplicationView/ApplicationController.cs | 23 +++--- PartPreviewWindow/PlusTab/ExplorePanel.cs | 92 +++++++++++++++-------- 2 files changed, 72 insertions(+), 43 deletions(-) diff --git a/ApplicationView/ApplicationController.cs b/ApplicationView/ApplicationController.cs index 6dd70679e..880ffd824 100644 --- a/ApplicationView/ApplicationController.cs +++ b/ApplicationView/ApplicationController.cs @@ -1124,24 +1124,23 @@ namespace MatterHackers.MatterControl } } - public static Task LoadCacheableAsync(string cacheKey, string cacheScope, string staticDataFallbackPath = null) where T : class + public string LoadCachedFile(string cacheKey, string cacheScope) { string cachePath = CacheablePath(cacheScope, cacheKey); - try + if (File.Exists(cachePath)) { - if (File.Exists(cachePath)) - { - // Load from cache and deserialize - return Task.FromResult( - JsonConvert.DeserializeObject(File.ReadAllText(cachePath))); - } - } - catch - { - // Fall back to StaticData + // Load from cache and deserialize + return File.ReadAllText(cachePath); } + return null; + } + + public static Task LoadCacheableAsync(string cacheKey, string cacheScope, string staticDataFallbackPath = null) where T : class + { + string cachePath = CacheablePath(cacheScope, cacheKey); + try { if (staticDataFallbackPath != null diff --git a/PartPreviewWindow/PlusTab/ExplorePanel.cs b/PartPreviewWindow/PlusTab/ExplorePanel.cs index 039c94706..38a502888 100644 --- a/PartPreviewWindow/PlusTab/ExplorePanel.cs +++ b/PartPreviewWindow/PlusTab/ExplorePanel.cs @@ -1,5 +1,5 @@ /* -Copyright (c) 2017, Lars Brubaker, John Lewin +Copyright (c) 2018, Lars Brubaker, John Lewin All rights reserved. Redistribution and use in source and binary forms, with or without @@ -38,7 +38,6 @@ using MatterHackers.Agg; using MatterHackers.Agg.Font; using MatterHackers.Agg.Image; using MatterHackers.Agg.UI; -using MatterHackers.MatterControl.SlicerConfiguration; using MatterHackers.VectorMath; using Newtonsoft.Json; @@ -46,14 +45,14 @@ namespace MatterHackers.MatterControl.PartPreviewWindow.PlusTab { public class ExplorePanel : FlowLayoutWidget { - string sk; + string relativeUrl; string staticFile; private ThemeConfig theme; - public ExplorePanel(ThemeConfig theme, string sk, string staticFile) + public ExplorePanel(ThemeConfig theme, string relativeUrl, string staticFile) : base(FlowDirection.TopToBottom) { - this.sk = sk; + this.relativeUrl = relativeUrl; this.staticFile = staticFile; this.HAnchor = HAnchor.Stretch; this.VAnchor = VAnchor.Fit; @@ -62,23 +61,49 @@ namespace MatterHackers.MatterControl.PartPreviewWindow.PlusTab this.theme = theme; } - public async override void OnLoad(EventArgs args) + public override void OnLoad(EventArgs args) { base.OnLoad(args); try { - var explorerFeed = await LoadExploreFeed(); + ExplorerFeed explorerFeed = null; - UiThread.RunOnIdle(() => + var json = ApplicationController.Instance.LoadCachedFile(staticFile, "MatterHackers"); + if (json != null) { + // Construct directly from cache + explorerFeed = JsonConvert.DeserializeObject(json); // Add controls for content AddControlsForContent(explorerFeed); - // Force layout to change to get it working - var oldMargin = this.Margin; - this.Margin = new BorderDouble(20); - this.Margin = oldMargin; + } + // Force layout to change to get it working + var oldMargin = this.Margin; + this.Margin = new BorderDouble(20); + this.Margin = oldMargin; + + Task.Run(async () => + { + string cachePath = ApplicationController.CacheablePath("MatterHackers", staticFile); + + // Construct directly from cache + explorerFeed = await LoadExploreFeed(json?.GetHashCode() ?? 0, cachePath); + + if (explorerFeed != null) + { + UiThread.RunOnIdle(() => + { + this.CloseAllChildren(); + + // Add controls for content + AddControlsForContent(explorerFeed); + + // Force layout to change to get it working + this.Margin = new BorderDouble(20); + this.Margin = oldMargin; + }); + } }); } catch @@ -86,28 +111,33 @@ namespace MatterHackers.MatterControl.PartPreviewWindow.PlusTab } } - public async Task LoadExploreFeed() + public async Task LoadExploreFeed(int loadedID, string cachePath) { - return await ApplicationController.LoadCacheableAsync( - staticFile, - "MatterHackers", - async () => + try + { + var client = new HttpClient(); + string json = await client.GetStringAsync($"http://www.matterhackers.com/feeds/{relativeUrl}"); + + var explorerFeed = JsonConvert.DeserializeObject(json); + + var sanitizedJsonID = JsonConvert.SerializeObject(explorerFeed, Formatting.Indented).GetHashCode(); + if (loadedID == sanitizedJsonID) { - try - { - var client = new HttpClient(); - string json = await client.GetStringAsync($"http://www.matterhackers.com/feeds/{sk}"); - - return JsonConvert.DeserializeObject(json); - } - catch(Exception ex) - { - Trace.WriteLine("Error collecting or loading feed: " + ex.Message); - } - return null; - }, - Path.Combine("OEMSettings", staticFile)); + } + + // update cache on success + File.WriteAllText(cachePath, JsonConvert.SerializeObject(explorerFeed, Formatting.Indented)); + + return explorerFeed; + + } + catch (Exception ex) + { + Trace.WriteLine("Error collecting or loading feed: " + ex.Message); + } + + return null; } private void AddControlsForContent(ExplorerFeed contentList)