diff --git a/MTGOSDK/src/API/Client.cs b/MTGOSDK/src/API/Client.cs index 4825a97a..14438fab 100644 --- a/MTGOSDK/src/API/Client.cs +++ b/MTGOSDK/src/API/Client.cs @@ -7,6 +7,7 @@ using System.Security; using System.Security.Authentication; +using MTGOSDK.API.Collection; using MTGOSDK.API.Users; using MTGOSDK.API.Interface; using MTGOSDK.Core; @@ -180,6 +181,7 @@ public Client(ClientOptions options = default) : base( public void ClearCaches() { UserManager.Users.Clear(); + CollectionManager.Cards.Clear(); } /// diff --git a/MTGOSDK/src/API/Collection/CollectionManager.cs b/MTGOSDK/src/API/Collection/CollectionManager.cs index 6b6915bf..6a7fa3de 100644 --- a/MTGOSDK/src/API/Collection/CollectionManager.cs +++ b/MTGOSDK/src/API/Collection/CollectionManager.cs @@ -3,6 +3,7 @@ SPDX-License-Identifier: Apache-2.0 **/ +using System.Collections.Concurrent; using System.Collections.Generic; using System.Reflection; @@ -33,6 +34,11 @@ public static class CollectionManager // ICardDefinition wrapper methods // + /// + /// A dictionary of cached Card objects. + /// + public static ConcurrentDictionary Cards { get; } = new(); + /// /// Returns a list of catalog ids for the given card name. /// @@ -49,12 +55,19 @@ public static IList GetCardIds(string cardName) => /// /// Thrown if no card is found with the given catalog id. /// - public static Card GetCard(int id) => - new( - s_cardDataManager.GetCardDefinitionForCatId(id) + public static Card GetCard(int id) + { + if (!Cards.TryGetValue(id, out var card)) + { + Cards[id] = card = new Card( + s_cardDataManager.GetCardDefinitionForCatId(id) ?? throw new KeyNotFoundException( $"No card found with catalog id #{id}.") - ); + ); + } + + return card; + } /// /// Returns a card object by the given card name.