private void saveCache(String path, ListingCache cache) { if (!flags.HasFlag(Options.Nocache)) { if (flags.HasFlag(Options.Verbose)) { Console.WriteLine($"Saving cache to {path}"); } String cacheJSON = JsonConvert.SerializeObject(cache); File.WriteAllText(path, cacheJSON); } }
/// <summary> /// Call before using the cache dictionary to ensure it is up to date /// </summary> /// <returns></returns> public ListingCache getCache() { ListingCache cache; DirectoryInfo pathInfo = new DirectoryInfo(cachePath); Action <ListingCache> cacheUpdate = (c) => { // Gets the latest cache from github if (flags.HasFlag(Options.Verbose)) { Console.WriteLine("Updating cache in memory from Github..."); } c.Update( Listing.FromJson( WebFetch.fetch(apiURL) ) ); if (flags.HasFlag(Options.Verbose)) { Console.WriteLine("Cache updated!"); } }; if (File.Exists(pathInfo.FullName)) { // Load cache from file cache = JsonConvert.DeserializeObject <ListingCache>(File.ReadAllText(pathInfo.FullName)); cache.flags = flags; // Get info branch info from Github Branch master = Branch.FromJson(WebFetch.fetch(branchURL)); if (flags.HasFlag(Options.Verbose)) { Console.WriteLine($"Cache successfully loaded from {pathInfo.FullName}"); } // Get the latest commits timestamp DateTimeOffset lastCommitDate = master.Commit.Commit.Author.Date; // Check the timestamp // Could probably get by just checking the last changed time on the file instead of from cache timestamp? if (DateTimeOffset.Compare(lastCommitDate, cache.TimeStamp) <= 0) { if (flags.HasFlag(Options.Verbose)) { Console.WriteLine($"Cache is outdated."); } cacheUpdate(cache); saveCache(pathInfo.FullName, cache); } else if (flags.HasFlag(Options.Verbose)) { Console.WriteLine($"Cache up to date with Github, no updates needed."); } } else { // Cache file doesn't exist, so create it and (if we're allowed) save it // and (if we're allowed) save it to ~/.getignore.cache if (flags.HasFlag(Options.Verbose)) { Console.WriteLine($"No cache found, creating in memory..."); } cache = new ListingCache(flags); cacheUpdate(cache); saveCache(pathInfo.FullName, cache); } return(cache); }