templateCacheCapacity top-level property

int get templateCacheCapacity

How many parsed templates each mini-language keeps, 512 by default.

Parsing a template costs far more than formatting with one already parsed — around three quarters of a first call — so the cache is what makes repeated formatting cheap. It is bounded because templates can come from data, and an unbounded cache would then be an unbounded leak.

Raise it when the working set is larger than the default and templates repeat; a set that cycles past the capacity keeps roughly capacity / size of itself resident.

A workload whose templates never repeat needs no setting: after enough misses in a row that each had to evict something, the cache stops being consulted and starts being consulted again once the workload gives it a reason. What it already holds is kept, not discarded. Setting this to zero still says the same thing outright, and says it from the first call.

This is a bound on entries, not on memory: see templateCacheMemoryLimit, which bounds the other one. Both apply.

Lowering it discards entries immediately. The caches are per isolate, and shared by every Format instance, which is safe because a parsed template does not depend on the engine that parsed it.

Throws ArgumentError when set below zero. That is a plain Dart argument error rather than a member of the FormattingException hierarchy on purpose: nothing is being formatted here. The typed hierarchy describes failures of a formatting call, and a caller catching it around one should not also be catching a mistake in its own configuration.

Implementation

int get templateCacheCapacity => _templateCacheCapacity;
set templateCacheCapacity (int value)

Implementation

set templateCacheCapacity(int value) {
  if (value < 0) {
    throw ArgumentError.value(value, 'templateCacheCapacity', 'Must be >= 0.');
  }
  _templateCacheCapacity = value;
  // New bounds, new answer to "does the working set fit": what the policy
  // concluded under the old ones no longer applies.
  _braceTemplateCache
    ..resume()
    ..trim();
  _printfTemplateCache
    ..resume()
    ..trim();
}