withTimeToLive method

CachedValue<CacheContentType> withTimeToLive({
  1. required Duration lifetime,
})

Wraps the declared CachedValue with a TimeToLiveCachedValue.

Usage example:

int factorial(int n) {
  if (n < 0) throw ('Negative numbers are not allowed.');
  return n <= 1 ? 1 : n * factorial(n - 1);
}

int originalValue = 1;
final factorialCache = CachedValue(
  () => factorial(originalValue),
).withTimeToLive(
  lifetime: Duration(seconds: 3),
);

originalValue = 6;
print(factorialCache.value); // 1

await Future.delayed(Duration(seconds: 3));

print(factorialCache.value); // 720

Implementation

CachedValue<CacheContentType> withTimeToLive({
  required Duration lifetime,
}) =>
    TimeToLiveCachedValue._(this, lifetime);