json_cache.dart 783 B

123456789101112131415161718192021222324252627282930313233343536
  1. import 'dart:io';
  2. import 'package:path_provider/path_provider.dart';
  3. @Deprecated("only kept to remove its files after migration")
  4. abstract class JsonCache<T> {
  5. final String cacheFileName;
  6. JsonCache(this.cacheFileName);
  7. Future<File> _getCacheFile() async {
  8. final basePath = await getTemporaryDirectory();
  9. final basePathName = basePath.path;
  10. final file = File("$basePathName/$cacheFileName.bin");
  11. return file;
  12. }
  13. Future<bool> isValid() async {
  14. final file = await _getCacheFile();
  15. return await file.exists();
  16. }
  17. Future<void> invalidate() async {
  18. try {
  19. final file = await _getCacheFile();
  20. await file.delete();
  21. } on FileSystemException {
  22. // file is already deleted
  23. }
  24. }
  25. void put(T data);
  26. Future<T?> get();
  27. }