public async Task HandleProductUpserted(Product product, DateTime timestamp) { if (timestamp < _legacyAssetsCache.CacheInitTimestamp) { return; } if (!product.IsStarted) { return; } var assets = await _legacyAssetsService.GetLegacyAssets(new List <string> { product.ProductId }); string oldMdsCodeIfUpdated = null; try { await _semaphore.WaitAsync(); var current = _legacyAssetsCache.GetById(product.ProductId); if (current != null && current.UnderlyingMdsCode != product.UnderlyingMdsCode) { oldMdsCodeIfUpdated = current.UnderlyingMdsCode; } _legacyAssetsCache.AddOrUpdateMultiple(assets); //We must have one asset for productId var asset = assets.FirstOrDefault(); if (asset == null) { _log.WriteWarning(nameof(LegacyAssetsCacheUpdater), nameof(HandleProductUpserted), $"We received ProductChanged with productId: {product.ProductId} but cannot find it in DB to update LegacyAssetCache"); return; } await PublishAssetUpsertedEvent(asset, oldMdsCodeIfUpdated); } finally { _semaphore.Release(); } }
public void Start() { _lockSlim.EnterWriteLock(); try { _log.WriteInfo(nameof(LegacyAssetCache), nameof(Start), "Asset(Legacy) Cache init started."); CacheInitTimestamp = DateTime.UtcNow; var response = _legacyAssetsService.GetLegacyAssets().GetAwaiter().GetResult(); _cache = response.ToDictionary(a => a.AssetId, v => v); } finally { _lockSlim.ExitWriteLock(); } }