protected Consumer<CloseableReference<CloseableImage>> wrapConsumer(
      final Consumer<CloseableReference<CloseableImage>> consumer,
      final CacheKey cacheKey,
      final boolean isBitmapCacheEnabledForWrite) {
    return new DelegatingConsumer<
        CloseableReference<CloseableImage>, CloseableReference<CloseableImage>>(consumer) {
      @Override
      public void onNewResultImpl(
          @Nullable CloseableReference<CloseableImage> newResult, @Status int status) {
        try {
          if (FrescoSystrace.isTracing()) {
            FrescoSystrace.beginSection("BitmapMemoryCacheProducer#onNewResultImpl");
          }
          final boolean isLast = isLast(status);
          // ignore invalid intermediate results and forward the null result if last
          if (newResult == null) {
            if (isLast) {
              getConsumer().onNewResult(null, status);
            }
            return;
          }
          // stateful and partial results cannot be cached and are just forwarded
          if (newResult.get().isStateful() || statusHasFlag(status, IS_PARTIAL_RESULT)) {
            getConsumer().onNewResult(newResult, status);
            return;
          }
          // if the intermediate result is not of a better quality than the cached result,
          // forward the already cached result and don't cache the new result.
          if (!isLast) {
            CloseableReference<CloseableImage> currentCachedResult = mMemoryCache.get(cacheKey);
            if (currentCachedResult != null) {
              try {
                QualityInfo newInfo = newResult.get().getQualityInfo();
                QualityInfo cachedInfo = currentCachedResult.get().getQualityInfo();
                if (cachedInfo.isOfFullQuality()
                    || cachedInfo.getQuality() >= newInfo.getQuality()) {
                  getConsumer().onNewResult(currentCachedResult, status);
                  return;
                }
              } finally {
                CloseableReference.closeSafely(currentCachedResult);
              }
            }
          }
          // cache, if needed, and forward the new result
          CloseableReference<CloseableImage> newCachedResult = null;
          if (isBitmapCacheEnabledForWrite) {
            newCachedResult = mMemoryCache.cache(cacheKey, newResult);
          }
          try {
            if (isLast) {
              getConsumer().onProgressUpdate(1f);
            }
            getConsumer()
                .onNewResult((newCachedResult != null) ? newCachedResult : newResult, status);
          } finally {
            CloseableReference.closeSafely(newCachedResult);
          }
        } finally {
          if (FrescoSystrace.isTracing()) {
            FrescoSystrace.endSection();
          }
        }
      }
    };
  }
Memory cache producer for the bitmap memory cache.
imagepipeline/src/main/java/com/facebook/imagepipeline/producers/BitmapMemoryCacheProducer.java