Cleaned up caching used inside HandbookHelper

Removed INT cast when returning value from `FromRUB`
Renamed `HydrateLookup` and made protected
Made `LookupCollection` protected and moved inside `HandbookHelper
This commit is contained in:
Chomp
2025-06-18 15:02:21 +01:00
parent 4c8d8d9440
commit be87e78ac0
@@ -15,15 +15,20 @@ public class HandbookHelper(
ICloner _cloner
)
{
protected LookupCollection _handbookPriceCache = new();
protected ItemConfig _itemConfig = _configServer.GetConfig<ItemConfig>();
protected bool _lookupCacheGenerated;
private LookupCollection? _handbookPriceCache;
protected virtual LookupCollection HandbookPriceCache
{
get { return _handbookPriceCache ??= HydrateHandbookCache(); }
}
protected readonly ItemConfig _itemConfig = _configServer.GetConfig<ItemConfig>();
/// <summary>
/// Create an in-memory cache of all items with associated handbook price in handbookPriceCache class
/// </summary>
public void HydrateLookup()
protected LookupCollection HydrateHandbookCache()
{
var result = new LookupCollection();
var handbook = _databaseService.GetHandbook();
// Add handbook overrides found in items.json config into db
foreach (var (key, priceOverride) in _itemConfig.HandbookPriceOverride)
@@ -52,30 +57,32 @@ public class HandbookHelper(
var handbookDbClone = _cloner.Clone(handbook);
foreach (var handbookItem in handbookDbClone.Items)
{
_handbookPriceCache.Items.ById.TryAdd(handbookItem.Id, handbookItem.Price ?? 0);
if (!_handbookPriceCache.Items.ByParent.TryGetValue(handbookItem.ParentId, out _))
result.Items.ById.TryAdd(handbookItem.Id, handbookItem.Price ?? 0);
if (!result.Items.ByParent.TryGetValue(handbookItem.ParentId, out _))
{
_handbookPriceCache.Items.ByParent.TryAdd(handbookItem.ParentId, []);
result.Items.ByParent.TryAdd(handbookItem.ParentId, []);
}
_handbookPriceCache.Items.ByParent.TryGetValue(handbookItem.ParentId, out var array);
array.Add(handbookItem.Id);
result.Items.ByParent.TryGetValue(handbookItem.ParentId, out var itemIds);
itemIds.Add(handbookItem.Id);
}
foreach (var handbookCategory in handbookDbClone.Categories)
{
_handbookPriceCache.Categories.ById.TryAdd(handbookCategory.Id, handbookCategory.ParentId);
result.Categories.ById.TryAdd(handbookCategory.Id, handbookCategory.ParentId);
if (handbookCategory.ParentId is not null)
{
if (!_handbookPriceCache.Categories.ByParent.TryGetValue(handbookCategory.ParentId, out _))
if (!result.Categories.ByParent.TryGetValue(handbookCategory.ParentId, out _))
{
_handbookPriceCache.Categories.ByParent.TryAdd(handbookCategory.ParentId, []);
result.Categories.ByParent.TryAdd(handbookCategory.ParentId, []);
}
_handbookPriceCache.Categories.ByParent.TryGetValue(handbookCategory.ParentId, out var array);
array.Add(handbookCategory.Id);
result.Categories.ByParent.TryGetValue(handbookCategory.ParentId, out var itemIds);
itemIds.Add(handbookCategory.Id);
}
}
return result;
}
/// <summary>
@@ -86,15 +93,9 @@ public class HandbookHelper(
/// <returns>price in roubles</returns>
public double GetTemplatePrice(string tpl)
{
if (!_lookupCacheGenerated)
if (HandbookPriceCache.Items.ById.TryGetValue(tpl, out var itemPrice))
{
HydrateLookup();
_lookupCacheGenerated = true;
}
if (_handbookPriceCache.Items.ById.TryGetValue(tpl, out var item))
{
return item;
return itemPrice;
}
var handbookItem = _databaseService.GetHandbook().Items?.FirstOrDefault(item => item.Id == tpl);
@@ -102,22 +103,29 @@ public class HandbookHelper(
{
const int newValue = 0;
if (!_handbookPriceCache.Items.ById.TryAdd(tpl, newValue))
if (!HandbookPriceCache.Items.ById.TryAdd(tpl, newValue))
{
_handbookPriceCache.Items.ById[tpl] = newValue;
// Overwrite
HandbookPriceCache.Items.ById[tpl] = newValue;
}
return newValue;
}
if (!_handbookPriceCache.Items.ById.TryAdd(tpl, handbookItem.Price ?? 0))
if (!HandbookPriceCache.Items.ById.TryAdd(tpl, handbookItem.Price ?? 0))
{
_handbookPriceCache.Items.ById[tpl] = handbookItem.Price ?? 0;
// Overwrite
HandbookPriceCache.Items.ById[tpl] = handbookItem.Price ?? 0;
}
return handbookItem.Price.Value;
}
/// <summary>
/// Sum price of supplied items with handbook prices
/// </summary>
/// <param name="items">Items to Sum</param>
/// <returns></returns>
public double GetTemplatePriceForItems(List<Item> items)
{
var total = 0D;
@@ -136,7 +144,7 @@ public class HandbookHelper(
/// <returns>string array</returns>
public List<string> TemplatesWithParent(string parentId)
{
_handbookPriceCache.Items.ByParent.TryGetValue(parentId, out var template);
HandbookPriceCache.Items.ByParent.TryGetValue(parentId, out var template);
return template ?? [];
}
@@ -148,7 +156,7 @@ public class HandbookHelper(
/// <returns>true if exists in cache</returns>
public bool IsCategory(string category)
{
return _handbookPriceCache.Categories.ById.TryGetValue(category, out _);
return HandbookPriceCache.Categories.ById.TryGetValue(category, out _);
}
/// <summary>
@@ -158,7 +166,7 @@ public class HandbookHelper(
/// <returns>string array</returns>
public List<string> ChildrenCategories(string categoryParent)
{
_handbookPriceCache.Categories.ByParent.TryGetValue(categoryParent, out var category);
HandbookPriceCache.Categories.ByParent.TryGetValue(categoryParent, out var category);
return category ?? [];
}
@@ -185,7 +193,7 @@ public class HandbookHelper(
{
if (currencyTypeTo == Money.ROUBLES)
{
return (int) roubleCurrencyCount;
return roubleCurrencyCount;
}
// Get price of currency from handbook
@@ -199,46 +207,46 @@ public class HandbookHelper(
{
return _databaseService.GetHandbook().Categories.FirstOrDefault(category => category.Id == handbookId);
}
}
public class LookupItem<T, I>
{
public LookupItem()
protected record LookupItem<T, I>
{
ById = new Dictionary<string, T>();
ByParent = new Dictionary<string, List<I>>();
public LookupItem()
{
ById = new Dictionary<string, T>();
ByParent = new Dictionary<string, List<I>>();
}
public Dictionary<string, T> ById
{
get;
set;
}
public Dictionary<string, List<I>> ByParent
{
get;
set;
}
}
public Dictionary<string, T> ById
protected record LookupCollection
{
get;
set;
}
public LookupCollection()
{
Items = new LookupItem<double, string>();
Categories = new LookupItem<string, string>();
}
public Dictionary<string, List<I>> ByParent
{
get;
set;
}
}
public class LookupCollection
{
public LookupCollection()
{
Items = new LookupItem<double, string>();
Categories = new LookupItem<string, string>();
}
public LookupItem<double, string> Items
{
get;
set;
}
public LookupItem<string, string> Categories
{
get;
set;
public LookupItem<double, string> Items
{
get;
set;
}
public LookupItem<string, string> Categories
{
get;
set;
}
}
}