handleCache = async function (directory, params) {
  const {
    source,
    options = {},
    cacheIdentifier,
    cacheDirectory,
    cacheCompression,
    hash,
    getFileTimestamp,
    logger,
  } = params;

  const file = path.join(
    directory,
    filename(source, cacheIdentifier, options, hash),
  );

  try {
    // No errors mean that the file was previously cached
    // we just need to return it
    logger.debug(`reading cache file '${file}'`);
    const result = await read(file, cacheCompression);
    if (
      !(await areExternalDependenciesModified(
        result.externalDependencies,
        getFileTimestamp,
      ))
    ) {
      logger.debug(`validated cache file '${file}'`);
      return result;
    }
    logger.debug(
      `discarded cache file '${file}' due to changes in external dependencies`,
    );
  } catch {
    // conitnue if cache can't be read
    logger.debug(`discarded cache as it can not be read`);
  }

  const fallback =
    typeof cacheDirectory !== "string" && directory !== os.tmpdir();

  // Make sure the directory exists.
  try {
    // overwrite directory if exists
    logger.debug(`creating cache folder '${directory}'`);
    await mkdir(directory, { recursive: true });
  } catch (err) {
    if (fallback) {
      return handleCache(os.tmpdir(), params);
    }

    throw err;
  }

  // Otherwise just transform the file
  // return it to the user asap and write it in cache
  logger.debug(`applying Babel transform`);
  const result = await transform(source, options);
  await addTimestamps(result.externalDependencies, getFileTimestamp);

  try {
    logger.debug(`writing result to cache file '${file}'`);
    await write(file, cacheCompression, result);
  } catch (err) {
    if (fallback) {
      // Fallback to tmpdir if node_modules folder not writable
      return handleCache(os.tmpdir(), params);
    }

    throw err;
  }

  return result;
}
Handle the cache

@params {String} directory
@params {Object} params