public async Task <(decimal summaryPrice, decimal discount)> CalculateTotalPriceAsync(Bucket bucket, Guid userId, bool force = false) { string priceCacheKey = "bucketPrice" + bucket.Id; string discountCacheKey = "bucketDiscount" + bucket.Id; var precalculatePrice = await _distributedCache.GetAsync(priceCacheKey); var precalculateDiscount = await _distributedCache.GetAsync(discountCacheKey); if (!force && precalculatePrice != null && precalculateDiscount != null) { return(BitconverterExt.ToDecimal(precalculatePrice), BitconverterExt.ToDecimal(precalculateDiscount)); } else { var response = await _pricingClient.PriceAsync(userId, new PriceRequestDTO { Products = bucket.Items.Select(g => new PProductDTO { ProductId = g.ProductId.ToString(), Quantity = g.Quantity }).ToArray() }); _distributedCache.Set(priceCacheKey, BitconverterExt.GetBytes(response.SummaryPrice), new DistributedCacheEntryOptions { AbsoluteExpirationRelativeToNow = new TimeSpan(0, 5, 0) }); _distributedCache.Set(discountCacheKey, BitconverterExt.GetBytes(response.Discount), new DistributedCacheEntryOptions { AbsoluteExpirationRelativeToNow = new TimeSpan(0, 5, 0) }); return(response.SummaryPrice, response.Discount); } }
public static T BitConvertTo <T>(this byte[] bytes, int offset = 0) { var type = typeof(T); if (type == typeof(sbyte)) { return(((sbyte)bytes[offset]).As <T>()); } if (type == typeof(byte)) { return(bytes[offset].As <T>()); } if (type == typeof(short)) { return(BitConverter.ToInt16(bytes, offset).As <T>()); } if (type == typeof(ushort)) { return(BitConverter.ToUInt16(bytes, offset).As <T>()); } if (type == typeof(int)) { return(BitConverter.ToInt32(bytes, offset).As <T>()); } if (type == typeof(uint)) { return(BitConverter.ToUInt32(bytes, offset).As <T>()); } if (type == typeof(long)) { return(BitConverter.ToInt64(bytes, offset).As <T>()); } if (type == typeof(ulong)) { return(BitConverter.ToUInt64(bytes, offset).As <T>()); } if (type == typeof(float)) { return(BitConverter.ToSingle(bytes, offset).As <T>()); } if (type == typeof(double)) { return(BitConverter.ToDouble(bytes, offset).As <T>()); } if (type == typeof(decimal)) { return(BitconverterExt.ToDecimal(bytes).As <T>()); } if (type == typeof(char)) { return(BitConverter.ToChar(bytes, offset).As <T>()); } if (type == typeof(string)) { return(BitConverter.ToString(bytes, offset).As <T>()); } throw new NotImplementedException(); }