get method

  1. @override
Future<Uint8List?> get({
  1. required String key,
})
override

Retrieves the data cached under the spefied key if available, null otherwise.

Implementation

@override
Future<Uint8List?> get({required String key}) async {
  Directory tempDir = await getTemporaryDirectory();

  String tempPath = tempDir.path;
  String base64Key = base64.encode(utf8.encode(key));

  File cacheDetailsFile = File('$tempPath/$base64Key.json');
  File cacheContentFile = File('$tempPath/$base64Key.bin');

  if (!cacheDetailsFile.existsSync()) {
    // TODO Revisit this.
    return null;
  }

  CacheDetails cacheDetails = CacheDetails.fromJson(jsonDecode(
      await cacheDetailsFile.readAsString()));

  if (!cacheContentFile.existsSync()) {
    return null;
  }

  final timestamp = cacheDetails.timestamp;
  final int maxAge = cacheDetails.maxAge;

  final int currentTime = DateTime
      .now()
      .millisecondsSinceEpoch;

  if (currentTime - timestamp <= maxAge) {
      return cacheContentFile.readAsBytes();
  }
  else {
    await cacheContentFile.delete();
  }

  return null;
}