private PriceCalculatorResult GetPriceFromErp(Guid variantSystemId) { var cacheKey = $"{nameof(ERPPriceCalculatorImpl)}:{variantSystemId}"; if (_distributedMemoryCacheService.TryGet <PriceCalculatorResult>(cacheKey, out var price)) { return(price); } using (_distributedLockService.AcquireLock(cacheKey, TimeSpan.FromSeconds(10))) { // Try getting value from cache again since it was likely added while we were waiting for the lock if (_distributedMemoryCacheService.TryGet <PriceCalculatorResult>(cacheKey, out price)) { return(price); } // Test the effect of the cache by faking a slow ERP API taking one second to get a variants price: Thread.Sleep(800); price = new PriceCalculatorResult { ListPrice = 100, VatPercentage = (decimal)0.25 }; _distributedMemoryCacheService.Set(cacheKey, price); } return(price); }
private PriceCalculatorResult GetPriceFromErp(Guid variantSystemId) { var cacheKey = $"{nameof(ERPPriceCalculatorImpl)}:{variantSystemId}"; if (_distributedMemoryCacheService.TryGet <PriceCalculatorResult>(cacheKey, out var price)) { return(price); } // Test the effect of the cache by faking a slow ERP API taking one second to get a variants price: Thread.Sleep(800); price = new PriceCalculatorResult { ListPrice = 100, VatPercentage = (decimal)0.25 }; _distributedMemoryCacheService.Set(cacheKey, price); return(price); }
private PriceCalculatorResult GetPriceFromErp(Guid variantSystemId) { var cacheKey = $"{nameof(ErpPriceCalculatorDecorator)}:{variantSystemId}"; if (_distributedMemoryCacheService.TryGet <PriceCalculatorResult>(cacheKey, out var price)) { _logger.LogDebug("Getting price from CACHE for variant {VariantSystemId}", variantSystemId); return(price); } using (_distributedLockService.AcquireLock(cacheKey, TimeSpan.FromSeconds(10))) { // Try getting value from cache again since it may have been added // from another thread/app while we were waiting for the lock if (_distributedMemoryCacheService.TryGet(cacheKey, out price)) { _logger.LogDebug("Getting price from CACHE for variant {VariantSystemId}", variantSystemId); return(price); } _logger.LogDebug("Getting price from ERP for variant {VariantSystemId}", variantSystemId); // Test the effect of the cache by faking a slow API: Thread.Sleep(500); price = new PriceCalculatorResult { PriceExcludingVat = 100, PriceIncludesVat = false, VatRate = (decimal)0.25 }; _distributedMemoryCacheService.Set(cacheKey, price); } return(price); }