put method

  1. @override
Future<void> put({
  1. required String key,
  2. required Uint8List bytes,
  3. Duration maxAge = const Duration(days: 30),
})
override

Adds the specified data to the cache under the specified key.

Implementation

@override
Future<void> put({required String key, required Uint8List bytes, Duration maxAge = const Duration(days: 30)}) async {
  Directory tempDir = await getTemporaryDirectory();

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

  final timestamp = DateTime.now().millisecondsSinceEpoch;
  CacheDetails cacheDetails =
  CacheDetails(timestamp: timestamp, maxAge: maxAge.inMilliseconds);

  // Write cache details as a json file.
  File cacheDetailsFile = File('$tempPath/$base64Key.json');
  await cacheDetailsFile.writeAsString(jsonEncode(cacheDetails.toJson()));

  // Write content as a file.
  File cacheContentFile = File('$tempPath/$base64Key.bin');
  await cacheContentFile.writeAsBytes(bytes);

}