cache<T> method

Future<T> cache<T>(
  1. Future<T> futureProvider(),
  2. String key
)

Caches futures key cacheKey

Implementation

Future<T> cache<T>(Future<T> Function() futureProvider, String key) async {
  final cached = _futureCache[key] as Future<T>?;
  Future<T> future;
  if (cached != null) {
    future = cached;
  } else {
    future = futureProvider();
    _futureCache[key] = future;
  }

  if (analyze) {
    if (cached == null) {
      print("       > $key");
      final start = DateTime.now();
      final result = await future;
      final diff = DateTime.now().difference(start);
      print("${diff.inMilliseconds.toString().padLeft(4)}ms < $key");
      return result;
    } else {
      print("       - $key");
    }
  }

  return future;
}