preload method
Reads key (and its version key) into memory, so that a store
constructed next hydrates without touching the disk. A value larger
than cacheBytes is not kept in memory, so it is not read here either:
the store reads it from its file.
Implementation
Future<void> preload(String key) => _locked('preload', () async {
for (final k in [key, HydratedMobX.versionKeyOf(key)]) {
if (!_keys.contains(k)) continue;
if (_cache.remove(k) case final bytes?) {
_cache[k] = bytes; // Most recently used now.
continue;
}
try {
final file = _fileOf(k);
if (await file.length() > _cacheLimit) continue;
_cachePut(k, await file.readAsBytes());
} on PathNotFoundException {
_keys.remove(k); // Deleted behind this storage's back.
}
}
});