private static LottieTask<LottieComposition> cache(@Nullable final String cacheKey, Callable<LottieResult<LottieComposition>> callable,
      @Nullable Runnable onCached) {
    LottieTask<LottieComposition> task = null;
    final LottieComposition cachedComposition = cacheKey == null ? null : LottieCompositionCache.getInstance().get(cacheKey);
    if (cachedComposition != null) {
      task = new LottieTask<>(cachedComposition);
    }
    if (cacheKey != null && taskCache.containsKey(cacheKey)) {
      task = taskCache.get(cacheKey);
    }
    if (task != null) {
      if (onCached != null) {
        onCached.run();
      }
      return task;
    }

    task = new LottieTask<>(callable);
    if (cacheKey != null) {
      AtomicBoolean resultAlreadyCalled = new AtomicBoolean(false);
      task.addListener(result -> {
        taskCache.remove(cacheKey);
        resultAlreadyCalled.set(true);
        if (taskCache.size() == 0) {
          notifyTaskCacheIdleListeners(true);
        }
      });
      task.addFailureListener(result -> {
        taskCache.remove(cacheKey);
        resultAlreadyCalled.set(true);
        if (taskCache.size() == 0) {
          notifyTaskCacheIdleListeners(true);
        }
      });
      // It is technically possible for the task to finish and for the listeners to get called
      // before this code runs. If this happens, the task will be put in taskCache but never removed.
      // This would require this thread to be sleeping at exactly this point in the code
      // for long enough for the task to finish and call the listeners. Unlikely but not impossible.
      if (!resultAlreadyCalled.get()) {
        taskCache.put(cacheKey, task);
        if (taskCache.size() == 1) {
          notifyTaskCacheIdleListeners(false);
        }
      }
    }
    return task;
  }
First, check to see if there are any in-progress tasks associated with the cache key and return it if there is.
If not, create a new task for the callable.
Then, add the new task to the task cache and set up listeners so it gets cleared when done.
lottie/src/main/java/com/airbnb/lottie/LottieCompositionFactory.java