flcCachedFileLoader<K> function

FileLoader<K> flcCachedFileLoader<K>({
  1. required FileLoader<K> loader,
  2. required FileStore store,
  3. required FilePath pathProvider(
    1. K key
    ),
})

Implementation

FileLoader<K> flcCachedFileLoader<K>({
  required FileLoader<K> loader,
  required FileStore store,
  required FilePath Function(K key) pathProvider,
}) {
  final cache = Cache<K, Future<Future<Uint8List> Function()>>(
    (key) async {
      final path = pathProvider(key);

      if (!await store.exists(path)) {
        final data = await loader(key);
        await store.write(path, data);
      }

      return () => store.read(path);
    },
  );

  return (path) async {
    final fn = await cache.get(path);
    return await fn();
  };
}