From 84dc3dfdbca1d446eaa501efb51c44ed5ea41fb8 Mon Sep 17 00:00:00 2001 From: CWX Date: Fri, 11 Apr 2025 12:17:48 +0100 Subject: [PATCH] Moved BotLootCacheService to ConcurrentDictionary --- .../Services/BotLootCacheService.cs | 73 ++++++++----------- 1 file changed, 29 insertions(+), 44 deletions(-) diff --git a/Libraries/SPTarkov.Server.Core/Services/BotLootCacheService.cs b/Libraries/SPTarkov.Server.Core/Services/BotLootCacheService.cs index 870200e1..afa4a8ce 100644 --- a/Libraries/SPTarkov.Server.Core/Services/BotLootCacheService.cs +++ b/Libraries/SPTarkov.Server.Core/Services/BotLootCacheService.cs @@ -1,3 +1,4 @@ +using System.Collections.Concurrent; using SPTarkov.Server.Core.Generators; using SPTarkov.Server.Core.Helpers; using SPTarkov.Server.Core.Models.Common; @@ -20,18 +21,14 @@ public class BotLootCacheService( ICloner _cloner ) { - protected readonly Lock _lockObject = new(); - protected Dictionary _lootCache = new(); + protected ConcurrentDictionary _lootCache = new ConcurrentDictionary(); /// /// Remove cached bot loot data /// public void ClearCache() { - lock (_lockObject) - { - _lootCache.Clear(); - } + _lootCache.Clear(); } /// @@ -49,21 +46,16 @@ public class BotLootCacheService( BotType botJsonTemplate, MinMax? itemPriceMinMax = null) { - lock (_lockObject) + if (!BotRoleExistsInCache(botRole)) { - if (!BotRoleExistsInCache(botRole)) - { - InitCacheForBotRole(botRole); - AddLootToCache(botRole, isPmc, botJsonTemplate); - } + InitCacheForBotRole(botRole); + AddLootToCache(botRole, isPmc, botJsonTemplate); } Dictionary result = null; BotLootCache botRoleCache; - lock (_lockObject) - { - botRoleCache = _lootCache[botRole]; - } + + botRoleCache = _lootCache[botRole]; switch (lootType) { @@ -123,8 +115,7 @@ public class BotLootCacheService( if (itemPriceMinMax is not null) { - var filteredResult = result.Where( - i => + var filteredResult = result.Where(i => { var itemPrice = _itemHelper.GetItemPrice(i.Key); if (itemPriceMinMax?.Min is not null && itemPriceMinMax?.Max is not null) @@ -467,24 +458,19 @@ public class BotLootCacheService( } BotLootCache cacheForRole; - lock (_lockObject) - { - cacheForRole = _lootCache[botRole]; - - cacheForRole.HealingItems = healingItems; - cacheForRole.DrugItems = drugItems; - cacheForRole.FoodItems = foodItems; - cacheForRole.DrinkItems = drinkItems; - cacheForRole.CurrencyItems = currencyItems; - cacheForRole.StimItems = stimItems; - cacheForRole.GrenadeItems = grenadeItems; - - cacheForRole.SpecialItems = specialLootItems; - cacheForRole.BackpackLoot = filteredBackpackItems; - cacheForRole.PocketLoot = filteredPocketItems; - cacheForRole.VestLoot = filteredVestItems; - cacheForRole.SecureLoot = filteredSecureLoot; - } + cacheForRole = _lootCache[botRole]; + cacheForRole.HealingItems = healingItems; + cacheForRole.DrugItems = drugItems; + cacheForRole.FoodItems = foodItems; + cacheForRole.DrinkItems = drinkItems; + cacheForRole.CurrencyItems = currencyItems; + cacheForRole.StimItems = stimItems; + cacheForRole.GrenadeItems = grenadeItems; + cacheForRole.SpecialItems = specialLootItems; + cacheForRole.BackpackLoot = filteredBackpackItems; + cacheForRole.PocketLoot = filteredPocketItems; + cacheForRole.VestLoot = filteredVestItems; + cacheForRole.SecureLoot = filteredSecureLoot; } protected void AddItemsToPool(Dictionary poolToAddTo, Dictionary poolOfItemsToAdd) @@ -563,10 +549,7 @@ public class BotLootCacheService( /// true if they exist protected bool BotRoleExistsInCache(string botRole) { - lock (_lockObject) - { - return _lootCache.ContainsKey(botRole); - } + return _lootCache.ContainsKey(botRole); } /// @@ -575,9 +558,8 @@ public class BotLootCacheService( /// Bot role to hydrate protected void InitCacheForBotRole(string botRole) { - lock (_lockObject) - { - _lootCache.Add( + if ( + !_lootCache.TryAdd( botRole, new BotLootCache { @@ -596,7 +578,10 @@ public class BotLootCacheService( HealingItems = new Dictionary(), StimItems = new Dictionary() } - ); + ) + ) + { + _logger.Error($"Unable to add loot cache for bot role: {botRole}"); } }