Skip to content

Instantly share code, notes, and snippets.

@renefloor
Last active November 20, 2020 08:54
Show Gist options
  • Save renefloor/0337275a8246c69a09775776de7de112 to your computer and use it in GitHub Desktop.
Save renefloor/0337275a8246c69a09775776de7de112 to your computer and use it in GitHub Desktop.
CacheManager FileService with custom validity
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
import 'package:http/http.dart' as http;
import 'package:clock/clock.dart';
const cacheKey = 'cache';
final cacheManager = CacheManager(Config(
cacheKey,
fileService: CustomFileService(),
));
const validity = Duration(days: 7);
class CustomFileService extends FileService {
http.Client _httpClient;
CustomFileService({http.Client httpClient}) {
_httpClient = httpClient ?? http.Client();
}
@override
Future<FileServiceResponse> get(String url,
{Map<String, String> headers = const {}}) async {
final req = http.Request('GET', Uri.parse(url));
req.headers.addAll(headers);
final httpResponse = await _httpClient.send(req);
return HttpResponseWithValidity(httpResponse);
}
}
class HttpResponseWithValidity extends HttpGetResponse {
final DateTime _receivedTime = clock.now();
HttpResponseWithValidity(http.StreamedResponse response) : super(response);
@override
DateTime get validTill => _receivedTime.add(validity);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment