cacheModel method
Cache a model
Stores binary data in Cache API and metadata in SharedPreferences. Uses URL normalization.
Throws if storage quota exceeded or other error.
Implementation
Future<void> cacheModel(String url, Uint8List data) async {
try {
final normalizedUrl = UrlUtils.normalizeUrl(url);
if (kDebugMode) {
debugPrint(
'WebCacheService: cacheModel($url) size: ${data.length} bytes, normalized: $normalizedUrl');
}
// Store in Cache API
await _cacheInterop.put(cacheName, normalizedUrl, data);
// Store metadata
await _saveMetadata(CacheMetadata(
url: url,
sizeInBytes: data.length,
timestamp: DateTime.now(),
cacheKey: normalizedUrl,
));
if (kDebugMode) {
debugPrint('[WebCacheService] ✅ Successfully cached $url');
}
} catch (e) {
debugPrint('[WebCacheService] ❌ cacheModel failed for $url: $e');
// Handle QuotaExceededError
if (e.toString().contains('quota')) {
debugPrint('[WebCacheService] ⚠️ Storage quota exceeded, attempting cleanup');
await _cleanupOldEntries();
// Retry once after cleanup
try {
final normalizedUrl = UrlUtils.normalizeUrl(url);
await _cacheInterop.put(cacheName, normalizedUrl, data);
await _saveMetadata(CacheMetadata(
url: url,
sizeInBytes: data.length,
timestamp: DateTime.now(),
cacheKey: normalizedUrl,
));
debugPrint('[WebCacheService] ✅ Cached after cleanup: $url');
return;
} catch (retryError) {
debugPrint('[WebCacheService] ❌ Retry failed: $retryError');
}
}
rethrow;
}
}