public async Task AddIncommingAsync <TSupplierType>(string incomingPrefix, TSupplierType supplierType, CancellationToken cancellationToken = default) { try { incomingPrefix.NotNullOrWhiteSpace(nameof(incomingPrefix)); supplierType.NotNull(nameof(supplierType)); var strSupplierType = supplierType.ConvertTo <string>(); Logger.SetProperty(LogConstants.SupplierType, strSupplierType); var cacheSetting = await CacheSettingService.GetFromCacheBySupplierTypeAsync(strSupplierType, cancellationToken).ConfigureAwait(false); var key = incomingPrefix + "-" + Guid.NewGuid().ToString("N"); await CacheManager.SetAsync(key, string.Empty, cacheSetting.RpmDurationMinutes, cancellationToken).ConfigureAwait(false); ////3 times to try //if (await TrySet().ConfigureAwait(false) == false) // if (await TrySet().ConfigureAwait(false) == false) // if (await TrySet().ConfigureAwait(false) == false) // throw new Exception($"Faild to set cache {nameof(AddSearchHistoryAsync)}"); //Task<bool> TrySet() => CacheManager.TrySetAsync(key, string.Empty, cacheSetting.RpmDurationMinutes, cancellationToken); } catch (Exception ex) when(Logger.LogErrorIfNotBefore(ex, "Exception ocurred in {MethodName}", nameof(AddIncommingAsync))) { throw; } }
public async Task <decimal> GetRpmAsync <TSupplierType>(string incomingPrefix, TSupplierType supplierType, CancellationToken cancellationToken = default) { try { incomingPrefix.NotNullOrWhiteSpace(nameof(incomingPrefix)); supplierType.NotNull(nameof(supplierType)); var hashCode = incomingPrefix.GetInvariantHashCode(); Logger.SetProperty(LogConstants.IncomingRequestHashCode, hashCode); Logger.SetProperty(LogConstants.SupplierType, supplierType.ConvertTo <string>()); var cacheSetting = await CacheSettingService.GetFromCacheBySupplierTypeAsync(supplierType, cancellationToken).ConfigureAwait(false); var rpmDurationDateTime = SystemClock.DateTimeNow.AddMinutes(-cacheSetting.RpmDurationMinutes); int count; using (await AsyncLock.LockAsync()) count = await IncomingRequestStore.TableNoTracking.CountAsync(p => p.HashCode == hashCode && p.CreatedAt >= rpmDurationDateTime).ConfigureAwait(false); var rpm = (decimal)count / cacheSetting.RpmDurationMinutes; Logger.SetProperty(LogConstants.CurrentRPM, rpm); return(rpm); } catch (Exception ex) when(Logger.LogErrorIfNotBefore(ex, "Exception ocurred in {MethodName}", nameof(GetRpmAsync))) { throw; } }