diff --git a/Libraries/SPTarkov.Server.Core/Loaders/BundleLoader.cs b/Libraries/SPTarkov.Server.Core/Loaders/BundleLoader.cs index f5328aa2..c7641ffe 100644 --- a/Libraries/SPTarkov.Server.Core/Loaders/BundleLoader.cs +++ b/Libraries/SPTarkov.Server.Core/Loaders/BundleLoader.cs @@ -65,6 +65,8 @@ public class BundleLoader(ISptLogger logger, JsonUtil jsonUtil, Bu AddBundle(bundleManifest.Key, new BundleInfo(relativeModPath, bundleManifest, bundleHash)); } + + await bundleHashCacheService.WriteCache(); } /// diff --git a/Libraries/SPTarkov.Server.Core/Services/BundleHashCacheService.cs b/Libraries/SPTarkov.Server.Core/Services/BundleHashCacheService.cs index 86f21806..05f56432 100644 --- a/Libraries/SPTarkov.Server.Core/Services/BundleHashCacheService.cs +++ b/Libraries/SPTarkov.Server.Core/Services/BundleHashCacheService.cs @@ -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 logger, J protected const string _bundleHashCachePath = "./user/cache/"; protected const string _cacheName = "bundleHashCache.json"; protected Dictionary _bundleHashes = []; + private readonly SemaphoreSlim _writeLock = new(1, 1); public async Task HydrateCache() { @@ -29,6 +31,27 @@ public class BundleHashCacheService(ISptLogger logger, J _bundleHashes = await jsonUtil.DeserializeFromFileAsync>(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 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"); } ///