Refactor bundle hash caching to save only after mod is loaded (#709)

This commit is contained in:
Archangel
2025-12-04 23:11:21 +01:00
parent 928508bd77
commit 1fd1a6d834
2 changed files with 26 additions and 10 deletions
@@ -65,6 +65,8 @@ public class BundleLoader(ISptLogger<BundleLoader> logger, JsonUtil jsonUtil, Bu
AddBundle(bundleManifest.Key, new BundleInfo(relativeModPath, bundleManifest, bundleHash));
}
await bundleHashCacheService.WriteCache();
}
/// <summary>
@@ -1,4 +1,5 @@
using SPTarkov.DI.Annotations;
using SPTarkov.Server.Core.Models.Eft.Profile;
using SPTarkov.Server.Core.Models.Utils;
using SPTarkov.Server.Core.Utils;
@@ -10,6 +11,7 @@ public class BundleHashCacheService(ISptLogger<BundleHashCacheService> logger, J
protected const string _bundleHashCachePath = "./user/cache/";
protected const string _cacheName = "bundleHashCache.json";
protected Dictionary<string, uint> _bundleHashes = [];
private readonly SemaphoreSlim _writeLock = new(1, 1);
public async Task HydrateCache()
{
@@ -29,6 +31,27 @@ public class BundleHashCacheService(ISptLogger<BundleHashCacheService> logger, J
_bundleHashes = await jsonUtil.DeserializeFromFileAsync<Dictionary<string, uint>>(fullCachePath) ?? [];
}
public async Task WriteCache()
{
await _writeLock.WaitAsync();
try
{
var bundleHashesSerialized = jsonUtil.Serialize(_bundleHashes);
if (bundleHashesSerialized is null)
{
return;
}
await fileUtil.WriteFileAsync(Path.Join(_bundleHashCachePath, _cacheName), bundleHashesSerialized);
}
finally
{
_writeLock.Release();
}
}
protected uint GetStoredValue(string key)
{
if (!_bundleHashes.TryGetValue(key, out var value))
@@ -43,16 +66,7 @@ public class BundleHashCacheService(ISptLogger<BundleHashCacheService> logger, J
{
_bundleHashes[bundlePath] = hash;
var bundleHashesSerialized = jsonUtil.Serialize(_bundleHashes);
if (bundleHashesSerialized is null)
{
return;
}
await fileUtil.WriteFileAsync(Path.Join(_bundleHashCachePath, _cacheName), bundleHashesSerialized);
logger.Debug($"Bundle: {bundlePath} hash stored in: ${_bundleHashCachePath}");
logger.Debug($"Bundle: {bundlePath} hash stored in cache");
}
/// <summary>