diff options
Diffstat (limited to 'src/SMAPI.Web/Framework/Caching')
-rw-r--r-- | src/SMAPI.Web/Framework/Caching/Cached.cs | 37 | ||||
-rw-r--r-- | src/SMAPI.Web/Framework/Caching/Mods/CachedMod.cs | 107 | ||||
-rw-r--r-- | src/SMAPI.Web/Framework/Caching/Mods/IModCacheRepository.cs | 9 | ||||
-rw-r--r-- | src/SMAPI.Web/Framework/Caching/Mods/ModCacheMemoryRepository.cs | 81 | ||||
-rw-r--r-- | src/SMAPI.Web/Framework/Caching/Mods/ModCacheRepository.cs | 104 | ||||
-rw-r--r-- | src/SMAPI.Web/Framework/Caching/UtcDateTimeOffsetSerializer.cs | 40 | ||||
-rw-r--r-- | src/SMAPI.Web/Framework/Caching/Wiki/CachedWikiMod.cs | 230 | ||||
-rw-r--r-- | src/SMAPI.Web/Framework/Caching/Wiki/IWikiCacheRepository.cs | 11 | ||||
-rw-r--r-- | src/SMAPI.Web/Framework/Caching/Wiki/WikiCacheMemoryRepository.cs | 53 | ||||
-rw-r--r-- | src/SMAPI.Web/Framework/Caching/Wiki/WikiCacheRepository.cs | 73 | ||||
-rw-r--r-- | src/SMAPI.Web/Framework/Caching/Wiki/WikiMetadata.cs (renamed from src/SMAPI.Web/Framework/Caching/Wiki/CachedWikiMetadata.cs) | 18 |
11 files changed, 182 insertions, 581 deletions
diff --git a/src/SMAPI.Web/Framework/Caching/Cached.cs b/src/SMAPI.Web/Framework/Caching/Cached.cs new file mode 100644 index 00000000..52041a16 --- /dev/null +++ b/src/SMAPI.Web/Framework/Caching/Cached.cs @@ -0,0 +1,37 @@ +using System; + +namespace StardewModdingAPI.Web.Framework.Caching +{ + /// <summary>A cache entry.</summary> + /// <typeparam name="T">The cached value type.</typeparam> + internal class Cached<T> + { + /********* + ** Accessors + *********/ + /// <summary>The cached data.</summary> + public T Data { get; set; } + + /// <summary>When the data was last updated.</summary> + public DateTimeOffset LastUpdated { get; set; } + + /// <summary>When the data was last requested through the mod API.</summary> + public DateTimeOffset LastRequested { get; set; } + + + /********* + ** Public methods + *********/ + /// <summary>Construct an empty instance.</summary> + public Cached() { } + + /// <summary>Construct an instance.</summary> + /// <param name="data">The cached data.</param> + public Cached(T data) + { + this.Data = data; + this.LastUpdated = DateTimeOffset.UtcNow; + this.LastRequested = DateTimeOffset.UtcNow; + } + } +} diff --git a/src/SMAPI.Web/Framework/Caching/Mods/CachedMod.cs b/src/SMAPI.Web/Framework/Caching/Mods/CachedMod.cs deleted file mode 100644 index 96eca847..00000000 --- a/src/SMAPI.Web/Framework/Caching/Mods/CachedMod.cs +++ /dev/null @@ -1,107 +0,0 @@ -using System; -using System.Diagnostics.CodeAnalysis; -using MongoDB.Bson; -using MongoDB.Bson.Serialization.Attributes; -using StardewModdingAPI.Toolkit.Framework.UpdateData; -using StardewModdingAPI.Web.Framework.ModRepositories; - -namespace StardewModdingAPI.Web.Framework.Caching.Mods -{ - /// <summary>The model for cached mod data.</summary> - internal class CachedMod - { - /********* - ** Accessors - *********/ - /**** - ** Tracking - ****/ - /// <summary>The internal MongoDB ID.</summary> - [SuppressMessage("ReSharper", "InconsistentNaming", Justification = "Named per MongoDB conventions.")] - [BsonIgnoreIfDefault] - public ObjectId _id { get; set; } - - /// <summary>When the data was last updated.</summary> - public DateTimeOffset LastUpdated { get; set; } - - /// <summary>When the data was last requested through the web API.</summary> - public DateTimeOffset LastRequested { get; set; } - - /**** - ** Metadata - ****/ - /// <summary>The mod site on which the mod is found.</summary> - public ModRepositoryKey Site { get; set; } - - /// <summary>The mod's unique ID within the <see cref="Site"/>.</summary> - public string ID { get; set; } - - /// <summary>The mod availability status on the remote site.</summary> - public RemoteModStatus FetchStatus { get; set; } - - /// <summary>The error message providing more info for the <see cref="FetchStatus"/>, if applicable.</summary> - public string FetchError { get; set; } - - - /**** - ** Mod info - ****/ - /// <summary>The mod's display name.</summary> - public string Name { get; set; } - - /// <summary>The mod's latest version.</summary> - public string MainVersion { get; set; } - - /// <summary>The mod's latest optional or prerelease version, if newer than <see cref="MainVersion"/>.</summary> - public string PreviewVersion { get; set; } - - /// <summary>The URL for the mod page.</summary> - public string Url { get; set; } - - /// <summary>The license URL, if available.</summary> - public string LicenseUrl { get; set; } - - /// <summary>The license name, if available.</summary> - public string LicenseName { get; set; } - - - /********* - ** Accessors - *********/ - /// <summary>Construct an instance.</summary> - public CachedMod() { } - - /// <summary>Construct an instance.</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> - public CachedMod(ModRepositoryKey site, string id, ModInfoModel mod) - { - // tracking - this.LastUpdated = DateTimeOffset.UtcNow; - this.LastRequested = DateTimeOffset.UtcNow; - - // metadata - this.Site = site; - this.ID = id; - this.FetchStatus = mod.Status; - this.FetchError = mod.Error; - - // mod info - this.Name = mod.Name; - this.MainVersion = mod.Version; - this.PreviewVersion = mod.PreviewVersion; - this.Url = mod.Url; - this.LicenseUrl = mod.LicenseUrl; - this.LicenseName = mod.LicenseName; - } - - /// <summary>Get the API model for the cached data.</summary> - public ModInfoModel GetModel() - { - return new ModInfoModel(name: this.Name, version: this.MainVersion, url: this.Url, previewVersion: this.PreviewVersion) - .SetLicense(this.LicenseUrl, this.LicenseName) - .SetError(this.FetchStatus, this.FetchError); - } - } -} diff --git a/src/SMAPI.Web/Framework/Caching/Mods/IModCacheRepository.cs b/src/SMAPI.Web/Framework/Caching/Mods/IModCacheRepository.cs index bcec8b36..0d912c7b 100644 --- a/src/SMAPI.Web/Framework/Caching/Mods/IModCacheRepository.cs +++ b/src/SMAPI.Web/Framework/Caching/Mods/IModCacheRepository.cs @@ -1,10 +1,10 @@ using System; using StardewModdingAPI.Toolkit.Framework.UpdateData; -using StardewModdingAPI.Web.Framework.ModRepositories; +using StardewModdingAPI.Web.Framework.Clients; 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 { /********* @@ -15,14 +15,13 @@ namespace StardewModdingAPI.Web.Framework.Caching.Mods /// <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> - bool TryGetMod(ModRepositoryKey site, string id, out CachedMod mod, bool markRequested = true); + bool TryGetMod(ModSiteKey site, string id, out Cached<IModPage> mod, bool markRequested = 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> - void SaveMod(ModRepositoryKey site, string id, ModInfoModel mod, out CachedMod cachedMod); + void SaveMod(ModSiteKey site, string id, IModPage 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> 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..6b0ec1ec --- /dev/null +++ b/src/SMAPI.Web/Framework/Caching/Mods/ModCacheMemoryRepository.cs @@ -0,0 +1,81 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using StardewModdingAPI.Toolkit.Framework.UpdateData; +using StardewModdingAPI.Web.Framework.Clients; + +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, Cached<IModPage>> Mods = new Dictionary<string, Cached<IModPage>>(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(ModSiteKey site, string id, out Cached<IModPage> mod, bool markRequested = true) + { + // get mod + if (!this.Mods.TryGetValue(this.GetKey(site, id), out var cachedMod)) + { + mod = null; + return false; + } + + // bump 'last requested' + if (markRequested) + cachedMod.LastRequested = DateTimeOffset.UtcNow; + + mod = cachedMod; + 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> + public void SaveMod(ModSiteKey site, string id, IModPage mod) + { + string key = this.GetKey(site, id); + this.Mods[key] = new Cached<IModPage>(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); + } + + + /********* + ** Private methods + *********/ + /// <summary>Get a cache key.</summary> + /// <param name="site">The mod site.</param> + /// <param name="id">The mod ID.</param> + private string GetKey(ModSiteKey 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/ModCacheRepository.cs deleted file mode 100644 index 2e7804a7..00000000 --- a/src/SMAPI.Web/Framework/Caching/Mods/ModCacheRepository.cs +++ /dev/null @@ -1,104 +0,0 @@ -using System; -using MongoDB.Driver; -using StardewModdingAPI.Toolkit.Framework.UpdateData; -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 - { - /********* - ** Fields - *********/ - /// <summary>The collection for cached mod data.</summary> - private readonly IMongoCollection<CachedMod> Mods; - - - /********* - ** Public methods - *********/ - /// <summary>Construct an instance.</summary> - /// <param name="database">The authenticated MongoDB database.</param> - public ModCacheRepository(IMongoDatabase database) - { - // get collections - this.Mods = database.GetCollection<CachedMod>("mods"); - - // add indexes if needed - this.Mods.Indexes.CreateOne(new CreateIndexModel<CachedMod>(Builders<CachedMod>.IndexKeys.Ascending(p => p.ID).Ascending(p => p.Site))); - } - - /********* - ** 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 - id = this.NormalizeId(id); - mod = this.Mods.Find(entry => entry.ID == id && entry.Site == site).FirstOrDefault(); - if (mod == null) - 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) - { - id = this.NormalizeId(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); - var result = 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) - { - string id = this.NormalizeId(mod.ID); - - this.Mods.ReplaceOne( - entry => entry.ID == id && entry.Site == mod.Site, - mod, - new UpdateOptions { IsUpsert = true } - ); - - return mod; - } - - /// <summary>Normalize a mod ID for case-insensitive search.</summary> - /// <param name="id">The mod ID.</param> - public string NormalizeId(string id) - { - return id.Trim().ToLower(); - } - } -} diff --git a/src/SMAPI.Web/Framework/Caching/UtcDateTimeOffsetSerializer.cs b/src/SMAPI.Web/Framework/Caching/UtcDateTimeOffsetSerializer.cs deleted file mode 100644 index 6a103e37..00000000 --- a/src/SMAPI.Web/Framework/Caching/UtcDateTimeOffsetSerializer.cs +++ /dev/null @@ -1,40 +0,0 @@ -using System; -using MongoDB.Bson; -using MongoDB.Bson.Serialization; -using MongoDB.Bson.Serialization.Serializers; - -namespace StardewModdingAPI.Web.Framework.Caching -{ - /// <summary>Serializes <see cref="DateTimeOffset"/> to a UTC date field instead of the default array.</summary> - public class UtcDateTimeOffsetSerializer : StructSerializerBase<DateTimeOffset> - { - /********* - ** Fields - *********/ - /// <summary>The underlying date serializer.</summary> - private static readonly DateTimeSerializer DateTimeSerializer = new DateTimeSerializer(DateTimeKind.Utc, BsonType.DateTime); - - - /********* - ** Public methods - *********/ - /// <summary>Deserializes a value.</summary> - /// <param name="context">The deserialization context.</param> - /// <param name="args">The deserialization args.</param> - /// <returns>A deserialized value.</returns> - public override DateTimeOffset Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args) - { - DateTime date = UtcDateTimeOffsetSerializer.DateTimeSerializer.Deserialize(context, args); - return new DateTimeOffset(date, TimeSpan.Zero); - } - - /// <summary>Serializes a value.</summary> - /// <param name="context">The serialization context.</param> - /// <param name="args">The serialization args.</param> - /// <param name="value">The object.</param> - public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, DateTimeOffset value) - { - UtcDateTimeOffsetSerializer.DateTimeSerializer.Serialize(context, args, value.UtcDateTime); - } - } -} diff --git a/src/SMAPI.Web/Framework/Caching/Wiki/CachedWikiMod.cs b/src/SMAPI.Web/Framework/Caching/Wiki/CachedWikiMod.cs deleted file mode 100644 index 7e7c99bc..00000000 --- a/src/SMAPI.Web/Framework/Caching/Wiki/CachedWikiMod.cs +++ /dev/null @@ -1,230 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics.CodeAnalysis; -using MongoDB.Bson; -using MongoDB.Bson.Serialization.Attributes; -using MongoDB.Bson.Serialization.Options; -using StardewModdingAPI.Toolkit; -using StardewModdingAPI.Toolkit.Framework.Clients.Wiki; - -namespace StardewModdingAPI.Web.Framework.Caching.Wiki -{ - /// <summary>The model for cached wiki mods.</summary> - internal class CachedWikiMod - { - /********* - ** Accessors - *********/ - /**** - ** Tracking - ****/ - /// <summary>The internal MongoDB ID.</summary> - [SuppressMessage("ReSharper", "InconsistentNaming", Justification = "Named per MongoDB conventions.")] - public ObjectId _id { get; set; } - - /// <summary>When the data was last updated.</summary> - public DateTimeOffset LastUpdated { get; set; } - - /**** - ** Mod info - ****/ - /// <summary>The mod's unique ID. If the mod has alternate/old IDs, they're listed in latest to newest order.</summary> - public string[] ID { get; set; } - - /// <summary>The mod's display name. If the mod has multiple names, the first one is the most canonical name.</summary> - public string[] Name { get; set; } - - /// <summary>The mod's author name. If the author has multiple names, the first one is the most canonical name.</summary> - public string[] Author { get; set; } - - /// <summary>The mod ID on Nexus.</summary> - public int? NexusID { get; set; } - - /// <summary>The mod ID in the Chucklefish mod repo.</summary> - public int? ChucklefishID { get; set; } - - /// <summary>The mod ID in the CurseForge mod repo.</summary> - public int? CurseForgeID { get; set; } - - /// <summary>The mod key in the CurseForge mod repo (used in mod page URLs).</summary> - public string CurseForgeKey { get; set; } - - /// <summary>The mod ID in the ModDrop mod repo.</summary> - public int? ModDropID { get; set; } - - /// <summary>The GitHub repository in the form 'owner/repo'.</summary> - public string GitHubRepo { get; set; } - - /// <summary>The URL to a non-GitHub source repo.</summary> - public string CustomSourceUrl { get; set; } - - /// <summary>The custom mod page URL (if applicable).</summary> - public string CustomUrl { get; set; } - - /// <summary>The name of the mod which loads this content pack, if applicable.</summary> - public string ContentPackFor { get; set; } - - /// <summary>The human-readable warnings for players about this mod.</summary> - public string[] Warnings { get; set; } - - /// <summary>The URL of the pull request which submits changes for an unofficial update to the author, if any.</summary> - public string PullRequestUrl { get; set; } - - /// <summary>Special notes intended for developers who maintain unofficial updates or submit pull requests. </summary> - public string DevNote { get; set; } - - /// <summary>The link anchor for the mod entry in the wiki compatibility list.</summary> - public string Anchor { get; set; } - - /**** - ** Stable compatibility - ****/ - /// <summary>The compatibility status.</summary> - public WikiCompatibilityStatus MainStatus { get; set; } - - /// <summary>The human-readable summary of the compatibility status or workaround, without HTML formatting.</summary> - public string MainSummary { get; set; } - - /// <summary>The game or SMAPI version which broke this mod (if applicable).</summary> - public string MainBrokeIn { get; set; } - - /// <summary>The version of the latest unofficial update, if applicable.</summary> - public string MainUnofficialVersion { get; set; } - - /// <summary>The URL to the latest unofficial update, if applicable.</summary> - public string MainUnofficialUrl { get; set; } - - /**** - ** Beta compatibility - ****/ - /// <summary>The compatibility status.</summary> - public WikiCompatibilityStatus? BetaStatus { get; set; } - - /// <summary>The human-readable summary of the compatibility status or workaround, without HTML formatting.</summary> - public string BetaSummary { get; set; } - - /// <summary>The game or SMAPI version which broke this mod (if applicable).</summary> - public string BetaBrokeIn { get; set; } - - /// <summary>The version of the latest unofficial update, if applicable.</summary> - public string BetaUnofficialVersion { get; set; } - - /// <summary>The URL to the latest unofficial update, if applicable.</summary> - public string BetaUnofficialUrl { get; set; } - - /**** - ** Version maps - ****/ - /// <summary>Maps local versions to a semantic version for update checks.</summary> - [BsonDictionaryOptions(Representation = DictionaryRepresentation.ArrayOfArrays)] - public IDictionary<string, string> MapLocalVersions { get; set; } - - /// <summary>Maps remote versions to a semantic version for update checks.</summary> - [BsonDictionaryOptions(Representation = DictionaryRepresentation.ArrayOfArrays)] - public IDictionary<string, string> MapRemoteVersions { get; set; } - - - /********* - ** Accessors - *********/ - /// <summary>Construct an instance.</summary> - public CachedWikiMod() { } - - /// <summary>Construct an instance.</summary> - /// <param name="mod">The mod data.</param> - public CachedWikiMod(WikiModEntry mod) - { - // tracking - this.LastUpdated = DateTimeOffset.UtcNow; - - // mod info - this.ID = mod.ID; - this.Name = mod.Name; - this.Author = mod.Author; - this.NexusID = mod.NexusID; - this.ChucklefishID = mod.ChucklefishID; - this.CurseForgeID = mod.CurseForgeID; - this.CurseForgeKey = mod.CurseForgeKey; - this.ModDropID = mod.ModDropID; - this.GitHubRepo = mod.GitHubRepo; - this.CustomSourceUrl = mod.CustomSourceUrl; - this.CustomUrl = mod.CustomUrl; - this.ContentPackFor = mod.ContentPackFor; - this.PullRequestUrl = mod.PullRequestUrl; - this.Warnings = mod.Warnings; - this.DevNote = mod.DevNote; - this.Anchor = mod.Anchor; - - // stable compatibility - this.MainStatus = mod.Compatibility.Status; - this.MainSummary = mod.Compatibility.Summary; - this.MainBrokeIn = mod.Compatibility.BrokeIn; - this.MainUnofficialVersion = mod.Compatibility.UnofficialVersion?.ToString(); - this.MainUnofficialUrl = mod.Compatibility.UnofficialUrl; - - // beta compatibility - this.BetaStatus = mod.BetaCompatibility?.Status; - this.BetaSummary = mod.BetaCompatibility?.Summary; - this.BetaBrokeIn = mod.BetaCompatibility?.BrokeIn; - this.BetaUnofficialVersion = mod.BetaCompatibility?.UnofficialVersion?.ToString(); - this.BetaUnofficialUrl = mod.BetaCompatibility?.UnofficialUrl; - - // version maps - this.MapLocalVersions = mod.MapLocalVersions; - this.MapRemoteVersions = mod.MapRemoteVersions; - } - - /// <summary>Reconstruct the original model.</summary> - public WikiModEntry GetModel() - { - var mod = new WikiModEntry - { - ID = this.ID, - Name = this.Name, - Author = this.Author, - NexusID = this.NexusID, - ChucklefishID = this.ChucklefishID, - CurseForgeID = this.CurseForgeID, - CurseForgeKey = this.CurseForgeKey, - ModDropID = this.ModDropID, - GitHubRepo = this.GitHubRepo, - CustomSourceUrl = this.CustomSourceUrl, - CustomUrl = this.CustomUrl, - ContentPackFor = this.ContentPackFor, - Warnings = this.Warnings, - PullRequestUrl = this.PullRequestUrl, - DevNote = this.DevNote, - Anchor = this.Anchor, - - // stable compatibility - Compatibility = new WikiCompatibilityInfo - { - Status = this.MainStatus, - Summary = this.MainSummary, - BrokeIn = this.MainBrokeIn, - UnofficialVersion = this.MainUnofficialVersion != null ? new SemanticVersion(this.MainUnofficialVersion) : null, - UnofficialUrl = this.MainUnofficialUrl - }, - - // version maps - MapLocalVersions = this.MapLocalVersions, - MapRemoteVersions = this.MapRemoteVersions - }; - - // beta compatibility - if (this.BetaStatus != null) - { - mod.BetaCompatibility = new WikiCompatibilityInfo - { - Status = this.BetaStatus.Value, - Summary = this.BetaSummary, - BrokeIn = this.BetaBrokeIn, - UnofficialVersion = this.BetaUnofficialVersion != null ? new SemanticVersion(this.BetaUnofficialVersion) : null, - UnofficialUrl = this.BetaUnofficialUrl - }; - } - - return mod; - } - } -} diff --git a/src/SMAPI.Web/Framework/Caching/Wiki/IWikiCacheRepository.cs b/src/SMAPI.Web/Framework/Caching/Wiki/IWikiCacheRepository.cs index b54c8a2f..2ab7ea5a 100644 --- a/src/SMAPI.Web/Framework/Caching/Wiki/IWikiCacheRepository.cs +++ b/src/SMAPI.Web/Framework/Caching/Wiki/IWikiCacheRepository.cs @@ -1,11 +1,10 @@ using System; using System.Collections.Generic; -using System.Linq.Expressions; 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 { /********* @@ -13,18 +12,16 @@ namespace StardewModdingAPI.Web.Framework.Caching.Wiki *********/ /// <summary>Get the cached wiki metadata.</summary> /// <param name="metadata">The fetched metadata.</param> - bool TryGetWikiMetadata(out CachedWikiMetadata metadata); + bool TryGetWikiMetadata(out Cached<WikiMetadata> metadata); /// <summary>Get the cached wiki mods.</summary> /// <param name="filter">A filter to apply, if any.</param> - IEnumerable<CachedWikiMod> GetWikiMods(Expression<Func<CachedWikiMod, bool>> filter = null); + IEnumerable<Cached<WikiModEntry>> GetWikiMods(Func<WikiModEntry, bool> filter = null); /// <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> - void SaveWikiData(string stableVersion, string betaVersion, IEnumerable<WikiModEntry> mods, out CachedWikiMetadata cachedMetadata, out CachedWikiMod[] cachedMods); + void SaveWikiData(string stableVersion, string betaVersion, IEnumerable<WikiModEntry> mods); } } 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..064a7c3c --- /dev/null +++ b/src/SMAPI.Web/Framework/Caching/Wiki/WikiCacheMemoryRepository.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.Linq; +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 Cached<WikiMetadata> Metadata; + + /// <summary>The cached wiki data.</summary> + private Cached<WikiModEntry>[] Mods = new Cached<WikiModEntry>[0]; + + + /********* + ** Public methods + *********/ + /// <summary>Get the cached wiki metadata.</summary> + /// <param name="metadata">The fetched metadata.</param> + public bool TryGetWikiMetadata(out Cached<WikiMetadata> 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<Cached<WikiModEntry>> GetWikiMods(Func<WikiModEntry, bool> filter = null) + { + foreach (var mod in this.Mods) + { + if (filter == null || filter(mod.Data)) + yield return mod; + } + } + + /// <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> + public void SaveWikiData(string stableVersion, string betaVersion, IEnumerable<WikiModEntry> mods) + { + this.Metadata = new Cached<WikiMetadata>(new WikiMetadata(stableVersion, betaVersion)); + this.Mods = mods.Select(mod => new Cached<WikiModEntry>(mod)).ToArray(); + } + } +} diff --git a/src/SMAPI.Web/Framework/Caching/Wiki/WikiCacheRepository.cs b/src/SMAPI.Web/Framework/Caching/Wiki/WikiCacheRepository.cs deleted file mode 100644 index 1ae9d38f..00000000 --- a/src/SMAPI.Web/Framework/Caching/Wiki/WikiCacheRepository.cs +++ /dev/null @@ -1,73 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Linq.Expressions; -using MongoDB.Driver; -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 - { - /********* - ** Fields - *********/ - /// <summary>The collection for wiki metadata.</summary> - private readonly IMongoCollection<CachedWikiMetadata> WikiMetadata; - - /// <summary>The collection for wiki mod data.</summary> - private readonly IMongoCollection<CachedWikiMod> WikiMods; - - - /********* - ** Public methods - *********/ - /// <summary>Construct an instance.</summary> - /// <param name="database">The authenticated MongoDB database.</param> - public WikiCacheRepository(IMongoDatabase database) - { - // get collections - this.WikiMetadata = database.GetCollection<CachedWikiMetadata>("wiki-metadata"); - this.WikiMods = database.GetCollection<CachedWikiMod>("wiki-mods"); - - // add indexes if needed - this.WikiMods.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(); - 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.WikiMods.Find(filter).ToList() - : this.WikiMods.Find("{}").ToList(); - } - - /// <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) - { - cachedMetadata = new CachedWikiMetadata(stableVersion, betaVersion); - cachedMods = mods.Select(mod => new CachedWikiMod(mod)).ToArray(); - - this.WikiMods.DeleteMany("{}"); - this.WikiMods.InsertMany(cachedMods); - - this.WikiMetadata.DeleteMany("{}"); - this.WikiMetadata.InsertOne(cachedMetadata); - } - } -} diff --git a/src/SMAPI.Web/Framework/Caching/Wiki/CachedWikiMetadata.cs b/src/SMAPI.Web/Framework/Caching/Wiki/WikiMetadata.cs index 6a560eb4..c04de4a5 100644 --- a/src/SMAPI.Web/Framework/Caching/Wiki/CachedWikiMetadata.cs +++ b/src/SMAPI.Web/Framework/Caching/Wiki/WikiMetadata.cs @@ -1,22 +1,11 @@ -using System; -using System.Diagnostics.CodeAnalysis; -using MongoDB.Bson; - namespace StardewModdingAPI.Web.Framework.Caching.Wiki { /// <summary>The model for cached wiki metadata.</summary> - internal class CachedWikiMetadata + internal class WikiMetadata { /********* ** Accessors *********/ - /// <summary>The internal MongoDB ID.</summary> - [SuppressMessage("ReSharper", "InconsistentNaming", Justification = "Named per MongoDB conventions.")] - public ObjectId _id { get; set; } - - /// <summary>When the data was last updated.</summary> - public DateTimeOffset LastUpdated { get; set; } - /// <summary>The current stable Stardew Valley version.</summary> public string StableVersion { get; set; } @@ -28,16 +17,15 @@ namespace StardewModdingAPI.Web.Framework.Caching.Wiki ** Public methods *********/ /// <summary>Construct an instance.</summary> - public CachedWikiMetadata() { } + public WikiMetadata() { } /// <summary>Construct an instance.</summary> /// <param name="stableVersion">The current stable Stardew Valley version.</param> /// <param name="betaVersion">The current beta Stardew Valley version.</param> - public CachedWikiMetadata(string stableVersion, string betaVersion) + public WikiMetadata(string stableVersion, string betaVersion) { this.StableVersion = stableVersion; this.BetaVersion = betaVersion; - this.LastUpdated = DateTimeOffset.UtcNow; } } } |