read method

String? read(
  1. String cacheDir,
  2. String namespace,
  3. String key, {
  4. required Duration ttl,
})

Reads a cached payload if it exists and is younger than ttl.

Implementation

String? read(
  String cacheDir,
  String namespace,
  String key, {
  required Duration ttl,
}) {
  final String path = p.join(cacheDir, namespace, '$key.json');
  if (!_fs.fileExists(path)) {
    return null;
  }
  final Map<String, dynamic> decoded =
      jsonDecode(_fs.readFile(path)) as Map<String, dynamic>;
  final DateTime storedAt = DateTime.parse(decoded['storedAt'] as String);
  if (DateTime.now().difference(storedAt) > ttl) {
    return null;
  }
  return decoded['payload'] as String?;
}