public static int[] GetBatchSizes( int valuesCount, int maxBatchSize, BatchBehaviour batchBehaviour) { var batchCount = ((valuesCount - 1) / maxBatchSize) + 1; var batchSizes = new int[batchCount]; if (batchBehaviour == BatchBehaviour.FillBatchesEvenly) { var averageBatchSize = (double)valuesCount / batchCount; var totalAllocated = 0; for (var index = 0; index < batchCount - 1; index++) { var nextBatchSize = (int)(averageBatchSize * (index + 1)) - totalAllocated; batchSizes[index] = nextBatchSize; totalAllocated += nextBatchSize; } var finalBatchSize = valuesCount - totalAllocated; batchSizes[batchCount - 1] = finalBatchSize; } else { for (var index = 0; index < batchCount - 1; index++) { batchSizes[index] = maxBatchSize; } var finalBatchSize = valuesCount - ((batchCount - 1) * maxBatchSize); batchSizes[batchCount - 1] = finalBatchSize; } return(batchSizes); }
public TConfig WithBatchedFetches(int maxBatchSize, BatchBehaviour batchBehaviour = BatchBehaviour.FillBatchesEvenly) { if (maxBatchSize <= 0) { throw new ArgumentOutOfRangeException(nameof(maxBatchSize)); } _config.MaxBatchSize = maxBatchSize; _config.MaxBatchSizeFactory = null; _config.BatchBehaviour = batchBehaviour; return((TConfig)this); }
public CachedFunctionWithOuterKeyAndInnerEnumerableKeys( Func <TParams, ReadOnlyMemory <TInnerKey>, CancellationToken, ValueTask <IEnumerable <KeyValuePair <TInnerKey, TValue> > > > originalFunction, Func <TParams, TOuterKey> keySelector, CachedFunctionWithOuterKeyAndInnerEnumerableKeysConfiguration <TParams, TOuterKey, TInnerKey, TValue> config) { _originalFunction = originalFunction; _keySelector = keySelector; _maxBatchSize = config.MaxBatchSize; _maxBatchSizeFactory = config.MaxBatchSizeFactory; _batchBehaviour = config.BatchBehaviour; _onSuccessAction = config.OnSuccessAction; _onExceptionAction = config.OnExceptionAction; _filterResponsePredicate = config.FilterResponsePredicate; if (config.DisableCaching) { _cache = NullCache <TOuterKey, TInnerKey, TValue> .Instance; } else { if (config.TimeToLive.HasValue) { _timeToLive = config.TimeToLive.Value; } else { _timeToLiveFactory = config.TimeToLiveFactory; } _cache = CacheBuilder.Build(config); _keyComparer = config.KeyComparer ?? EqualityComparer <TInnerKey> .Default; _skipCacheGetOuterPredicate = config.SkipCacheGetOuterPredicate; _skipCacheGetInnerPredicate = config.SkipCacheGetInnerPredicate; _skipCacheSetOuterPredicate = config.SkipCacheSetOuterPredicate; _skipCacheSetInnerPredicate = config.SkipCacheSetInnerPredicate; } if (config.FillMissingKeysConstantValue.IsSet) { _shouldFillMissingKeys = true; _shouldFillMissingKeysWithConstantValue = true; _fillMissingKeysConstantValue = config.FillMissingKeysConstantValue.Value; } else if (!(config.FillMissingKeysValueFactory is null)) { _shouldFillMissingKeys = true; _fillMissingKeysValueFactory = config.FillMissingKeysValueFactory; } _cacheEnabled = _cache.LocalCacheEnabled || _cache.DistributedCacheEnabled; _measurementsEnabled = _onSuccessAction != null || _onExceptionAction != null; }
private protected TConfig WithBatchedFetchesInternal(Func <TParams, ReadOnlyMemory <TKey>, int> maxBatchSizeFactory, BatchBehaviour batchBehaviour) { if (maxBatchSizeFactory is null) { throw new ArgumentNullException(nameof(maxBatchSizeFactory)); } _config.MaxBatchSizeFactory = maxBatchSizeFactory; _config.BatchBehaviour = batchBehaviour; return((TConfig)this); }