PdfBudgetedCache<K, V> constructor

PdfBudgetedCache<K, V>({
  1. int weigher(
    1. V value
    )?,
  2. int? maxWeight,
  3. int? maxEntries,
  4. void disposer(
    1. V value
    )?,
  5. V cloner(
    1. V value
    )?,
  6. void onEvicted(
    1. K key,
    2. V value
    )?,
  7. bool rejectOversize = false,
  8. bool clearsUnderMemoryPressure = false,
  9. String debugLabel = 'cache',
})

Creates a cache bounded by maxWeight (using weigher) and/or maxEntries. At least one bound should be given or the cache grows without limit. maxEntries is floored at 1 so the cache always keeps the most-recently-used entry. When clearsUnderMemoryPressure is set the cache registers with PdfCacheRegistry for its lifetime (until dispose).

Implementation

PdfBudgetedCache({
  int Function(V value)? weigher,
  int? maxWeight,
  int? maxEntries,
  void Function(V value)? disposer,
  V Function(V value)? cloner,
  void Function(K key, V value)? onEvicted,
  bool rejectOversize = false,
  bool clearsUnderMemoryPressure = false,
  this.debugLabel = 'cache',
})  : _weigher = weigher,
      _maxWeight = maxWeight ?? 0,
      _hasWeightBudget = weigher != null && maxWeight != null,
      _maxEntries = maxEntries == null ? null : math.max(1, maxEntries),
      _disposer = disposer,
      _cloner = cloner,
      _onEvicted = onEvicted,
      _rejectOversize = rejectOversize,
      _clearsUnderMemoryPressure = clearsUnderMemoryPressure {
  if (_clearsUnderMemoryPressure) PdfCacheRegistry.instance.register(this);
}