summaryrefslogtreecommitdiff
path: root/src/SMAPI/Framework/Utilities
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2022-05-22 14:38:57 -0400
committerJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2022-05-22 14:38:57 -0400
commit2ab2182645179129997eac3fccb63f6f0683dbe1 (patch)
treeddea4c6e4531a10c698d9757a57ae24c6732bff7 /src/SMAPI/Framework/Utilities
parent5731b015a0c548ac72e0d7ce9c4153aa52da3562 (diff)
parent336cc1cc0f250c96ee23d45e1e08569b67a2e562 (diff)
downloadSMAPI-2ab2182645179129997eac3fccb63f6f0683dbe1.tar.gz
SMAPI-2ab2182645179129997eac3fccb63f6f0683dbe1.tar.bz2
SMAPI-2ab2182645179129997eac3fccb63f6f0683dbe1.zip
Merge branch 'develop' into stable
Diffstat (limited to 'src/SMAPI/Framework/Utilities')
-rw-r--r--src/SMAPI/Framework/Utilities/TickCacheDictionary.cs26
1 files changed, 26 insertions, 0 deletions
diff --git a/src/SMAPI/Framework/Utilities/TickCacheDictionary.cs b/src/SMAPI/Framework/Utilities/TickCacheDictionary.cs
index 20d206e2..7732ace8 100644
--- a/src/SMAPI/Framework/Utilities/TickCacheDictionary.cs
+++ b/src/SMAPI/Framework/Utilities/TickCacheDictionary.cs
@@ -48,4 +48,30 @@ namespace StardewModdingAPI.Framework.Utilities
return this.Cache.Remove(cacheKey);
}
}
+
+ /// <summary>An in-memory dictionary cache that stores data for the duration of a game update tick.</summary>
+ /// <typeparam name="TKey">The dictionary key type.</typeparam>
+ internal class TickCacheDictionary<TKey> : TickCacheDictionary<TKey, object>
+ where TKey : notnull
+ {
+ /*********
+ ** Public methods
+ *********/
+ /// <summary>Get a value from the cache, fetching it first if it's not cached yet.</summary>
+ /// <param name="cacheKey">The unique key for the cached value.</param>
+ /// <param name="get">Get the latest data if it's not in the cache yet.</param>
+ public TValue GetOrSet<TValue>(TKey cacheKey, Func<TValue> get)
+ {
+ object? value = base.GetOrSet(cacheKey, () => get()!);
+
+ try
+ {
+ return (TValue)value;
+ }
+ catch (Exception ex)
+ {
+ throw new InvalidCastException($"Can't cast value of the '{cacheKey}' cache entry from {value?.GetType().FullName ?? "null"} to {typeof(TValue).FullName}.", ex);
+ }
+ }
+ }
}