public async Task <List <ManagedSite> > GetManagedSites(ManagedSiteFilter filter = null, bool reloadAll = true) { // Don't reload settings unless we need to or we are unsure if any items have changed if (!ManagedSitesCache.Any() || IsSingleInstanceMode == false || reloadAll) { await LoadAllManagedItems(); } // filter and convert dictionary to list TODO: use db instead of in memory filter? var items = ManagedSitesCache.Values.AsEnumerable(); if (filter != null) { if (!String.IsNullOrEmpty(filter.Keyword)) { items = items.Where(i => i.Name.ToLowerInvariant().Contains(filter.Keyword.ToLowerInvariant())); } //TODO: IncludeOnlyNextAutoRenew if (filter.MaxResults > 0) { items = items.Take(filter.MaxResults); } } return(new List <ManagedSite>(items)); }
public async Task LoadAllManagedItems(bool skipIfLoaded = false) { if (skipIfLoaded && ManagedSitesCache.Any()) { return; } await UpgradeSettings(); var watch = Stopwatch.StartNew(); // FIXME: this method should be async and called only when absolutely required, these // files can be hundreds of megabytes var path = GetDbPath(); if (File.Exists(path)) { var managedSites = new List <ManagedSite>(); using (var db = new SQLiteConnection($"Data Source={path}")) using (var cmd = new SQLiteCommand("SELECT id, json FROM manageditem", db)) { await db.OpenAsync(); using (var reader = await cmd.ExecuteReaderAsync()) { while (await reader.ReadAsync()) { string itemId = (string)reader["id"]; var managedSite = JsonConvert.DeserializeObject <ManagedSite>((string)reader["json"]); // in some cases users may have previously manipulated the id, causing // duplicates. Correct the ID here (database Id is unique): if (managedSite.Id != itemId) { managedSite.Id = itemId; Debug.WriteLine("LoadSettings: Corrected managed site id: " + managedSite.Name); } managedSites.Add(managedSite); } } } foreach (var site in managedSites) { site.IsChanged = false; } ManagedSitesCache = managedSites.ToDictionary(s => s.Id);; } else { ManagedSitesCache = new Dictionary <string, ManagedSite>(); } Debug.WriteLine($"LoadSettings[SQLite] took {watch.ElapsedMilliseconds}ms for {ManagedSitesCache.Count} records"); }
public async Task LoadAllManagedItems(bool skipIfLoaded = false) { if (skipIfLoaded && ManagedSitesCache.Any()) { return; } await UpgradeSettings(); var watch = Stopwatch.StartNew(); // FIXME: this method should be async and called only when absolutely required, these // files can be hundreds of megabytes var path = GetDbPath(); if (File.Exists(path)) { var managedSites = new List <ManagedSite>(); using (var db = new SQLiteConnection($"Data Source={path}")) using (var cmd = new SQLiteCommand("SELECT json FROM manageditem", db)) { await db.OpenAsync(); using (var reader = await cmd.ExecuteReaderAsync()) { while (await reader.ReadAsync()) { managedSites.Add(JsonConvert.DeserializeObject <ManagedSite>((string)reader["json"])); } } } foreach (var site in managedSites) { site.IsChanged = false; } ManagedSitesCache = managedSites.ToDictionary(s => s.Id); } else { ManagedSitesCache = new Dictionary <string, ManagedSite>(); } Debug.WriteLine($"LoadSettings[SQLite] took {watch.ElapsedMilliseconds}ms for {ManagedSitesCache.Count} records"); }
public async Task <ManagedSite> GetManagedSite(string siteId) { ManagedSite result = null; if (ManagedSitesCache == null || !ManagedSitesCache.Any()) { Debug.WriteLine("GetManagedSite: No managed sites loaded, will load item directly."); } else { // try to get cached version result = ManagedSitesCache.TryGetValue(siteId, out var retval) ? retval : null; } // if we don't have cached copy of info, load it from db if (result == null) { result = await LoadManagedSite(siteId); } return(result); }