writeIndex method
Implementation
Future<void> writeIndex() async {
// Try to acquire lock with longer timeout for write operations
if (!await _acquireLock(timeout: const Duration(seconds: 10))) {
// Log warning and return gracefully instead of throwing error
httpCacheLogger.warning(
'Failed to acquire lock for writing cache index for $url - skipping cache update');
return;
}
try {
final BytesBuilder bytesBuilder = BytesBuilder();
// Index bytes format:
// | Type x 1B | Version x 1B | Reserved x 2B |
bytesBuilder.add([
networkType,
_indexFormatVersion,
reserved,
reserved,
]);
// | ExpiredTimeStamp x 8B |
final int expiredTimeStamp = (expiredTime ?? alwaysExpired).millisecondsSinceEpoch;
writeInteger(bytesBuilder, expiredTimeStamp, 8);
// | LastUsedTimeStamp x 8B |
final int lastUsedTimeStamp = (lastUsed ?? DateTime.now()).millisecondsSinceEpoch;
writeInteger(bytesBuilder, lastUsedTimeStamp, 8);
// | LastModifiedTimeStamp x 8B |
final int lastModifiedTimestamp = (lastModified ?? alwaysExpired).millisecondsSinceEpoch;
writeInteger(bytesBuilder, lastModifiedTimestamp, 8);
// | ContentLength x 4B |
writeInteger(bytesBuilder, contentLength ?? 0, 4);
// | Length of url x 4B | URL Payload x N |
writeString(bytesBuilder, url, 4);
// | Length of eTag x 2B | eTag payload x N |
// Store url length, 2B max represents 2^16-1 bytes.
writeString(bytesBuilder, eTag ?? '', 2);
// | Length of shorted headers x 4B | shorted headers payload x N |
// Store shorted response headers, 4B.
writeString(bytesBuilder, headers ?? '', 4);
// | Length of content checksum x 2B | checksum payload x N |
// Store content checksum (SHA-256), 2B.
writeString(bytesBuilder, contentChecksum ?? '', 2);
// Write atomically using temp file + rename
await _writeFileAtomically(_file, bytesBuilder.takeBytes());
_valid = true;
// Update persisted timestamp after successful write
_lastUsedPersistedAt = lastUsed;
} finally {
// Always release lock
await _releaseLock();
}
}