touchMaxStale function
Future<CacheResponse>
touchMaxStale(
- CacheResponse entry,
- CacheOptions options,
- CacheStore store, {
- StreamingEntry? streamed,
Extends the entry lifetime by CacheOptions.maxStale.
Writes to store only when needed.
With streamed, writes the headers only.
Implementation
Future<CacheResponse> touchMaxStale(
CacheResponse entry,
CacheOptions options,
CacheStore store, {
StreamingEntry? streamed,
}) async {
final maxStale = options.maxStale;
if (maxStale == null) return entry;
final now = DateTime.now().toUtc();
final newMaxStale = now.add(maxStale);
final halfWindow = Duration(microseconds: maxStale.inMicroseconds ~/ 2);
final existing = entry.maxStale;
final needsWrite =
existing == null ||
existing.isBefore(now.add(halfWindow)) ||
newMaxStale.isBefore(existing);
entry = entry.copyWith(maxStale: newMaxStale);
if (needsWrite) {
if (streamed != null) {
await (store as StreamingCacheStore).setHeaders(streamed, entry);
} else {
await store.set(await entry.writeContent(options));
}
}
return entry;
}