diff options
Diffstat (limited to 'src/SMAPI.Web/Framework/Caching')
-rw-r--r-- | src/SMAPI.Web/Framework/Caching/Mods/IModCacheRepository.cs | 2 | ||||
-rw-r--r-- | src/SMAPI.Web/Framework/Caching/Mods/ModCacheMemoryRepository.cs | 89 | ||||
-rw-r--r-- | src/SMAPI.Web/Framework/Caching/Mods/ModCacheMongoRepository.cs (renamed from src/SMAPI.Web/Framework/Caching/Mods/ModCacheRepository.cs) | 19 | ||||
-rw-r--r-- | src/SMAPI.Web/Framework/Caching/Wiki/IWikiCacheRepository.cs | 2 | ||||
-rw-r--r-- | src/SMAPI.Web/Framework/Caching/Wiki/WikiCacheMemoryRepository.cs | 54 | ||||
-rw-r--r-- | src/SMAPI.Web/Framework/Caching/Wiki/WikiCacheMongoRepository.cs (renamed from src/SMAPI.Web/Framework/Caching/Wiki/WikiCacheRepository.cs) | 30 |
6 files changed, 170 insertions, 26 deletions
diff --git a/src/SMAPI.Web/Framework/Caching/Mods/IModCacheRepository.cs b/src/SMAPI.Web/Framework/Caching/Mods/IModCacheRepository.cs index bcec8b36..08749f3b 100644 --- a/src/SMAPI.Web/Framework/Caching/Mods/IModCacheRepository.cs +++ b/src/SMAPI.Web/Framework/Caching/Mods/IModCacheRepository.cs @@ -4,7 +4,7 @@ using StardewModdingAPI.Web.Framework.ModRepositories; namespace StardewModdingAPI.Web.Framework.Caching.Mods { - /// <summary>Encapsulates logic for accessing the mod data cache.</summary> + /// <summary>Manages cached mod data.</summary> internal interface IModCacheRepository : ICacheRepository { /********* diff --git a/src/SMAPI.Web/Framework/Caching/Mods/ModCacheMemoryRepository.cs b/src/SMAPI.Web/Framework/Caching/Mods/ModCacheMemoryRepository.cs new file mode 100644 index 00000000..9c5a217e --- /dev/null +++ b/src/SMAPI.Web/Framework/Caching/Mods/ModCacheMemoryRepository.cs @@ -0,0 +1,89 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using StardewModdingAPI.Toolkit.Framework.UpdateData; +using StardewModdingAPI.Web.Framework.ModRepositories; + +namespace StardewModdingAPI.Web.Framework.Caching.Mods +{ + /// <summary>Manages cached mod data in-memory.</summary> + internal class ModCacheMemoryRepository : BaseCacheRepository, IModCacheRepository + { + /********* + ** Fields + *********/ + /// <summary>The cached mod data indexed by <c>{site key}:{ID}</c>.</summary> + private readonly IDictionary<string, CachedMod> Mods = new Dictionary<string, CachedMod>(StringComparer.InvariantCultureIgnoreCase); + + + /********* + ** Public methods + *********/ + /// <summary>Get the cached mod data.</summary> + /// <param name="site">The mod site to search.</param> + /// <param name="id">The mod's unique ID within the <paramref name="site"/>.</param> + /// <param name="mod">The fetched mod.</param> + /// <param name="markRequested">Whether to update the mod's 'last requested' date.</param> + public bool TryGetMod(ModRepositoryKey site, string id, out CachedMod mod, bool markRequested = true) + { + // get mod + if (!this.Mods.TryGetValue(this.GetKey(site, id), out mod)) + return false; + + // bump 'last requested' + if (markRequested) + { + mod.LastRequested = DateTimeOffset.UtcNow; + mod = this.SaveMod(mod); + } + + return true; + } + + /// <summary>Save data fetched for a mod.</summary> + /// <param name="site">The mod site on which the mod is found.</param> + /// <param name="id">The mod's unique ID within the <paramref name="site"/>.</param> + /// <param name="mod">The mod data.</param> + /// <param name="cachedMod">The stored mod record.</param> + public void SaveMod(ModRepositoryKey site, string id, ModInfoModel mod, out CachedMod cachedMod) + { + string key = this.GetKey(site, id); + cachedMod = this.SaveMod(new CachedMod(site, id, mod)); + } + + /// <summary>Delete data for mods which haven't been requested within a given time limit.</summary> + /// <param name="age">The minimum age for which to remove mods.</param> + public void RemoveStaleMods(TimeSpan age) + { + DateTimeOffset minDate = DateTimeOffset.UtcNow.Subtract(age); + + string[] staleKeys = this.Mods + .Where(p => p.Value.LastRequested < minDate) + .Select(p => p.Key) + .ToArray(); + + foreach (string key in staleKeys) + this.Mods.Remove(key); + } + + /// <summary>Save data fetched for a mod.</summary> + /// <param name="mod">The mod data.</param> + public CachedMod SaveMod(CachedMod mod) + { + string key = this.GetKey(mod.Site, mod.ID); + return this.Mods[key] = mod; + } + + + /********* + ** Private methods + *********/ + /// <summary>Get a cache key.</summary> + /// <param name="site">The mod site.</param> + /// <param name="id">The mod ID.</param> + public string GetKey(ModRepositoryKey site, string id) + { + return $"{site}:{id.Trim()}".ToLower(); + } + } +} diff --git a/src/SMAPI.Web/Framework/Caching/Mods/ModCacheRepository.cs b/src/SMAPI.Web/Framework/Caching/Mods/ModCacheMongoRepository.cs index 2e7804a7..f105baab 100644 --- a/src/SMAPI.Web/Framework/Caching/Mods/ModCacheRepository.cs +++ b/src/SMAPI.Web/Framework/Caching/Mods/ModCacheMongoRepository.cs @@ -5,8 +5,8 @@ using StardewModdingAPI.Web.Framework.ModRepositories; namespace StardewModdingAPI.Web.Framework.Caching.Mods { - /// <summary>Encapsulates logic for accessing the mod data cache.</summary> - internal class ModCacheRepository : BaseCacheRepository, IModCacheRepository + /// <summary>Manages cached mod data in MongoDB.</summary> + internal class ModCacheMongoRepository : BaseCacheRepository, IModCacheRepository { /********* ** Fields @@ -20,7 +20,7 @@ namespace StardewModdingAPI.Web.Framework.Caching.Mods *********/ /// <summary>Construct an instance.</summary> /// <param name="database">The authenticated MongoDB database.</param> - public ModCacheRepository(IMongoDatabase database) + public ModCacheMongoRepository(IMongoDatabase database) { // get collections this.Mods = database.GetCollection<CachedMod>("mods"); @@ -29,6 +29,7 @@ namespace StardewModdingAPI.Web.Framework.Caching.Mods this.Mods.Indexes.CreateOne(new CreateIndexModel<CachedMod>(Builders<CachedMod>.IndexKeys.Ascending(p => p.ID).Ascending(p => p.Site))); } + /********* ** Public methods *********/ @@ -72,13 +73,9 @@ namespace StardewModdingAPI.Web.Framework.Caching.Mods public void RemoveStaleMods(TimeSpan age) { DateTimeOffset minDate = DateTimeOffset.UtcNow.Subtract(age); - var result = this.Mods.DeleteMany(p => p.LastRequested < minDate); + this.Mods.DeleteMany(p => p.LastRequested < minDate); } - - /********* - ** Private methods - *********/ /// <summary>Save data fetched for a mod.</summary> /// <param name="mod">The mod data.</param> public CachedMod SaveMod(CachedMod mod) @@ -88,12 +85,16 @@ namespace StardewModdingAPI.Web.Framework.Caching.Mods this.Mods.ReplaceOne( entry => entry.ID == id && entry.Site == mod.Site, mod, - new UpdateOptions { IsUpsert = true } + new ReplaceOptions { IsUpsert = true } ); return mod; } + + /********* + ** Private methods + *********/ /// <summary>Normalize a mod ID for case-insensitive search.</summary> /// <param name="id">The mod ID.</param> public string NormalizeId(string id) diff --git a/src/SMAPI.Web/Framework/Caching/Wiki/IWikiCacheRepository.cs b/src/SMAPI.Web/Framework/Caching/Wiki/IWikiCacheRepository.cs index b54c8a2f..02097f52 100644 --- a/src/SMAPI.Web/Framework/Caching/Wiki/IWikiCacheRepository.cs +++ b/src/SMAPI.Web/Framework/Caching/Wiki/IWikiCacheRepository.cs @@ -5,7 +5,7 @@ using StardewModdingAPI.Toolkit.Framework.Clients.Wiki; namespace StardewModdingAPI.Web.Framework.Caching.Wiki { - /// <summary>Encapsulates logic for accessing the wiki data cache.</summary> + /// <summary>Manages cached wiki data.</summary> internal interface IWikiCacheRepository : ICacheRepository { /********* diff --git a/src/SMAPI.Web/Framework/Caching/Wiki/WikiCacheMemoryRepository.cs b/src/SMAPI.Web/Framework/Caching/Wiki/WikiCacheMemoryRepository.cs new file mode 100644 index 00000000..4621f5e3 --- /dev/null +++ b/src/SMAPI.Web/Framework/Caching/Wiki/WikiCacheMemoryRepository.cs @@ -0,0 +1,54 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using StardewModdingAPI.Toolkit.Framework.Clients.Wiki; + +namespace StardewModdingAPI.Web.Framework.Caching.Wiki +{ + /// <summary>Manages cached wiki data in-memory.</summary> + internal class WikiCacheMemoryRepository : BaseCacheRepository, IWikiCacheRepository + { + /********* + ** Fields + *********/ + /// <summary>The saved wiki metadata.</summary> + private CachedWikiMetadata Metadata; + + /// <summary>The cached wiki data.</summary> + private CachedWikiMod[] Mods = new CachedWikiMod[0]; + + + /********* + ** Public methods + *********/ + /// <summary>Get the cached wiki metadata.</summary> + /// <param name="metadata">The fetched metadata.</param> + public bool TryGetWikiMetadata(out CachedWikiMetadata metadata) + { + metadata = this.Metadata; + return metadata != null; + } + + /// <summary>Get the cached wiki mods.</summary> + /// <param name="filter">A filter to apply, if any.</param> + public IEnumerable<CachedWikiMod> GetWikiMods(Expression<Func<CachedWikiMod, bool>> filter = null) + { + return filter != null + ? this.Mods.Where(filter.Compile()) + : this.Mods.ToArray(); + } + + /// <summary>Save data fetched from the wiki compatibility list.</summary> + /// <param name="stableVersion">The current stable Stardew Valley version.</param> + /// <param name="betaVersion">The current beta Stardew Valley version.</param> + /// <param name="mods">The mod data.</param> + /// <param name="cachedMetadata">The stored metadata record.</param> + /// <param name="cachedMods">The stored mod records.</param> + public void SaveWikiData(string stableVersion, string betaVersion, IEnumerable<WikiModEntry> mods, out CachedWikiMetadata cachedMetadata, out CachedWikiMod[] cachedMods) + { + this.Metadata = cachedMetadata = new CachedWikiMetadata(stableVersion, betaVersion); + this.Mods = cachedMods = mods.Select(mod => new CachedWikiMod(mod)).ToArray(); + } + } +} diff --git a/src/SMAPI.Web/Framework/Caching/Wiki/WikiCacheRepository.cs b/src/SMAPI.Web/Framework/Caching/Wiki/WikiCacheMongoRepository.cs index 1ae9d38f..07e7c721 100644 --- a/src/SMAPI.Web/Framework/Caching/Wiki/WikiCacheRepository.cs +++ b/src/SMAPI.Web/Framework/Caching/Wiki/WikiCacheMongoRepository.cs @@ -7,17 +7,17 @@ using StardewModdingAPI.Toolkit.Framework.Clients.Wiki; namespace StardewModdingAPI.Web.Framework.Caching.Wiki { - /// <summary>Encapsulates logic for accessing the wiki data cache.</summary> - internal class WikiCacheRepository : BaseCacheRepository, IWikiCacheRepository + /// <summary>Manages cached wiki data in MongoDB.</summary> + internal class WikiCacheMongoRepository : BaseCacheRepository, IWikiCacheRepository { /********* ** Fields *********/ /// <summary>The collection for wiki metadata.</summary> - private readonly IMongoCollection<CachedWikiMetadata> WikiMetadata; + private readonly IMongoCollection<CachedWikiMetadata> Metadata; /// <summary>The collection for wiki mod data.</summary> - private readonly IMongoCollection<CachedWikiMod> WikiMods; + private readonly IMongoCollection<CachedWikiMod> Mods; /********* @@ -25,21 +25,21 @@ namespace StardewModdingAPI.Web.Framework.Caching.Wiki *********/ /// <summary>Construct an instance.</summary> /// <param name="database">The authenticated MongoDB database.</param> - public WikiCacheRepository(IMongoDatabase database) + public WikiCacheMongoRepository(IMongoDatabase database) { // get collections - this.WikiMetadata = database.GetCollection<CachedWikiMetadata>("wiki-metadata"); - this.WikiMods = database.GetCollection<CachedWikiMod>("wiki-mods"); + this.Metadata = database.GetCollection<CachedWikiMetadata>("wiki-metadata"); + this.Mods = database.GetCollection<CachedWikiMod>("wiki-mods"); // add indexes if needed - this.WikiMods.Indexes.CreateOne(new CreateIndexModel<CachedWikiMod>(Builders<CachedWikiMod>.IndexKeys.Ascending(p => p.ID))); + this.Mods.Indexes.CreateOne(new CreateIndexModel<CachedWikiMod>(Builders<CachedWikiMod>.IndexKeys.Ascending(p => p.ID))); } /// <summary>Get the cached wiki metadata.</summary> /// <param name="metadata">The fetched metadata.</param> public bool TryGetWikiMetadata(out CachedWikiMetadata metadata) { - metadata = this.WikiMetadata.Find("{}").FirstOrDefault(); + metadata = this.Metadata.Find("{}").FirstOrDefault(); return metadata != null; } @@ -48,8 +48,8 @@ namespace StardewModdingAPI.Web.Framework.Caching.Wiki public IEnumerable<CachedWikiMod> GetWikiMods(Expression<Func<CachedWikiMod, bool>> filter = null) { return filter != null - ? this.WikiMods.Find(filter).ToList() - : this.WikiMods.Find("{}").ToList(); + ? this.Mods.Find(filter).ToList() + : this.Mods.Find("{}").ToList(); } /// <summary>Save data fetched from the wiki compatibility list.</summary> @@ -63,11 +63,11 @@ namespace StardewModdingAPI.Web.Framework.Caching.Wiki cachedMetadata = new CachedWikiMetadata(stableVersion, betaVersion); cachedMods = mods.Select(mod => new CachedWikiMod(mod)).ToArray(); - this.WikiMods.DeleteMany("{}"); - this.WikiMods.InsertMany(cachedMods); + this.Mods.DeleteMany("{}"); + this.Mods.InsertMany(cachedMods); - this.WikiMetadata.DeleteMany("{}"); - this.WikiMetadata.InsertOne(cachedMetadata); + this.Metadata.DeleteMany("{}"); + this.Metadata.InsertOne(cachedMetadata); } } } |