perf: async cache reads in content repository
This commit is contained in:
@@ -26,12 +26,12 @@ class ContentRepository {
|
|||||||
return SeasonPack.fromJson(jsonDecode(raw) as Map<String, dynamic>);
|
return SeasonPack.fromJson(jsonDecode(raw) as Map<String, dynamic>);
|
||||||
}
|
}
|
||||||
|
|
||||||
SeasonPack? _loadCachedSeason(Directory dir) {
|
Future<SeasonPack?> _loadCachedSeason(Directory dir) async {
|
||||||
try {
|
try {
|
||||||
final file = File('${dir.path}/pack.json');
|
final file = File('${dir.path}/pack.json');
|
||||||
if (!file.existsSync()) return null;
|
if (!await file.exists()) return null;
|
||||||
return SeasonPack.fromJson(
|
return SeasonPack.fromJson(
|
||||||
jsonDecode(file.readAsStringSync()) as Map<String, dynamic>);
|
jsonDecode(await file.readAsString()) as Map<String, dynamic>);
|
||||||
} catch (_) {
|
} catch (_) {
|
||||||
return null; // Corrupt or future-schema pack: ignore.
|
return null; // Corrupt or future-schema pack: ignore.
|
||||||
}
|
}
|
||||||
@@ -51,9 +51,10 @@ class ContentRepository {
|
|||||||
|
|
||||||
final seasonsDir =
|
final seasonsDir =
|
||||||
cacheDir == null ? null : Directory('${cacheDir!.path}/seasons');
|
cacheDir == null ? null : Directory('${cacheDir!.path}/seasons');
|
||||||
if (seasonsDir != null && seasonsDir.existsSync()) {
|
if (seasonsDir != null && await seasonsDir.exists()) {
|
||||||
for (final entry in seasonsDir.listSync().whereType<Directory>()) {
|
await for (final entry in seasonsDir.list()) {
|
||||||
final pack = _loadCachedSeason(entry);
|
if (entry is! Directory) continue;
|
||||||
|
final pack = await _loadCachedSeason(entry);
|
||||||
if (pack != null) byId[pack.seasonId] = pack;
|
if (pack != null) byId[pack.seasonId] = pack;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user