fetchHtmlWithCache method
Future<String>
fetchHtmlWithCache({
- required String url,
- required DataCacheManager cacheManager,
- Map<
String, String> ? headers, - int? timeout,
- int? retries,
- DataCacheOptions cacheOptions = const DataCacheOptions(),
Fetches HTML with caching
Implementation
Future<String> fetchHtmlWithCache({
required String url,
required DataCacheManager cacheManager,
Map<String, String>? headers,
int? timeout,
int? retries,
DataCacheOptions cacheOptions = const DataCacheOptions(),
}) async {
// Generate a cache key from the URL
final cacheKey = _generateCacheKey(url);
// Try to get the HTML from cache
final cachedHtml = await cacheManager.get<String>(cacheKey);
if (cachedHtml != null) {
logger.info('Cache hit for $url');
return cachedHtml;
}
// If not in cache, fetch the HTML
logger.info('Cache miss for $url, fetching...');
final html = await fetchHtml(
url: url,
headers: headers,
timeout: timeout,
retries: retries,
);
// Cache the HTML
await cacheManager.put<String>(cacheKey, html, options: cacheOptions);
return html;
}