summaryrefslogtreecommitdiff
path: root/src/SMAPI.Web/Framework/Caching/Mods
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2020-05-23 19:25:34 -0400
committerJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2020-05-23 19:25:34 -0400
commitd7add894419543667e60569bfeb439e8e797a4d1 (patch)
tree0b0b53e6ac306f1549951abe806bded7f9236bd1 /src/SMAPI.Web/Framework/Caching/Mods
parent9aba50451b617e1af8215358afda22c8105477f2 (diff)
downloadSMAPI-d7add894419543667e60569bfeb439e8e797a4d1.tar.gz
SMAPI-d7add894419543667e60569bfeb439e8e797a4d1.tar.bz2
SMAPI-d7add894419543667e60569bfeb439e8e797a4d1.zip
drop MongoDB code
MongoDB support unnecessarily complicated the code and there's no need to run distributed servers in the foreseeable future. This keeps the abstract storage interface so we can wrap a distributed cache in the future.
Diffstat (limited to 'src/SMAPI.Web/Framework/Caching/Mods')
-rw-r--r--src/SMAPI.Web/Framework/Caching/Mods/CachedMod.cs107
-rw-r--r--src/SMAPI.Web/Framework/Caching/Mods/IModCacheRepository.cs5
-rw-r--r--src/SMAPI.Web/Framework/Caching/Mods/ModCacheMemoryRepository.cs30
-rw-r--r--src/SMAPI.Web/Framework/Caching/Mods/ModCacheMongoRepository.cs105
4 files changed, 13 insertions, 234 deletions
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 08749f3b..004202f9 100644
--- a/src/SMAPI.Web/Framework/Caching/Mods/IModCacheRepository.cs
+++ b/src/SMAPI.Web/Framework/Caching/Mods/IModCacheRepository.cs
@@ -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(ModRepositoryKey site, string id, out Cached<ModInfoModel> 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(ModRepositoryKey site, string id, ModInfoModel 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
index 9c5a217e..62461116 100644
--- a/src/SMAPI.Web/Framework/Caching/Mods/ModCacheMemoryRepository.cs
+++ b/src/SMAPI.Web/Framework/Caching/Mods/ModCacheMemoryRepository.cs
@@ -13,7 +13,7 @@ namespace StardewModdingAPI.Web.Framework.Caching.Mods
** 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);
+ private readonly IDictionary<string, Cached<ModInfoModel>> Mods = new Dictionary<string, Cached<ModInfoModel>>(StringComparer.InvariantCultureIgnoreCase);
/*********
@@ -24,19 +24,20 @@ 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>
- public bool TryGetMod(ModRepositoryKey site, string id, out CachedMod mod, bool markRequested = true)
+ public bool TryGetMod(ModRepositoryKey site, string id, out Cached<ModInfoModel> mod, bool markRequested = true)
{
// get mod
- if (!this.Mods.TryGetValue(this.GetKey(site, id), out mod))
+ if (!this.Mods.TryGetValue(this.GetKey(site, id), out var cachedMod))
+ {
+ mod = null;
return false;
+ }
// bump 'last requested'
if (markRequested)
- {
- mod.LastRequested = DateTimeOffset.UtcNow;
- mod = this.SaveMod(mod);
- }
+ cachedMod.LastRequested = DateTimeOffset.UtcNow;
+ mod = cachedMod;
return true;
}
@@ -44,11 +45,10 @@ namespace StardewModdingAPI.Web.Framework.Caching.Mods
/// <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)
+ public void SaveMod(ModRepositoryKey site, string id, ModInfoModel mod)
{
string key = this.GetKey(site, id);
- cachedMod = this.SaveMod(new CachedMod(site, id, mod));
+ this.Mods[key] = new Cached<ModInfoModel>(mod);
}
/// <summary>Delete data for mods which haven't been requested within a given time limit.</summary>
@@ -66,14 +66,6 @@ namespace StardewModdingAPI.Web.Framework.Caching.Mods
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
@@ -81,7 +73,7 @@ namespace StardewModdingAPI.Web.Framework.Caching.Mods
/// <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)
+ private string GetKey(ModRepositoryKey site, string id)
{
return $"{site}:{id.Trim()}".ToLower();
}
diff --git a/src/SMAPI.Web/Framework/Caching/Mods/ModCacheMongoRepository.cs b/src/SMAPI.Web/Framework/Caching/Mods/ModCacheMongoRepository.cs
deleted file mode 100644
index f105baab..00000000
--- a/src/SMAPI.Web/Framework/Caching/Mods/ModCacheMongoRepository.cs
+++ /dev/null
@@ -1,105 +0,0 @@
-using System;
-using MongoDB.Driver;
-using StardewModdingAPI.Toolkit.Framework.UpdateData;
-using StardewModdingAPI.Web.Framework.ModRepositories;
-
-namespace StardewModdingAPI.Web.Framework.Caching.Mods
-{
- /// <summary>Manages cached mod data in MongoDB.</summary>
- internal class ModCacheMongoRepository : 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 ModCacheMongoRepository(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);
- this.Mods.DeleteMany(p => p.LastRequested < minDate);
- }
-
- /// <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 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)
- {
- return id.Trim().ToLower();
- }
- }
-}