Java Code Examples for com.facebook.imagepipeline.request.ImageRequest#getCacheChoice()

The following examples show how to use com.facebook.imagepipeline.request.ImageRequest#getCacheChoice() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DiskCacheReadProducer.java    From fresco with MIT License 6 votes vote down vote up
public void produceResults(
    final Consumer<EncodedImage> consumer, final ProducerContext producerContext) {
  final ImageRequest imageRequest = producerContext.getImageRequest();
  if (!imageRequest.isDiskCacheEnabled()) {
    maybeStartInputProducer(consumer, producerContext);
    return;
  }

  producerContext.getProducerListener().onProducerStart(producerContext, PRODUCER_NAME);

  final CacheKey cacheKey =
      mCacheKeyFactory.getEncodedCacheKey(imageRequest, producerContext.getCallerContext());
  final boolean isSmallRequest = (imageRequest.getCacheChoice() == CacheChoice.SMALL);
  final BufferedDiskCache preferredCache =
      isSmallRequest ? mSmallImageBufferedDiskCache : mDefaultBufferedDiskCache;
  final AtomicBoolean isCancelled = new AtomicBoolean(false);
  final Task<EncodedImage> diskLookupTask = preferredCache.get(cacheKey, isCancelled);
  final Continuation<EncodedImage, Void> continuation =
      onFinishDiskReads(consumer, producerContext);
  diskLookupTask.continueWith(continuation);
  subscribeTaskForRequestCancellation(isCancelled, producerContext);
}
 
Example 2
Source File: DiskCacheWriteProducer.java    From fresco with MIT License 5 votes vote down vote up
@Override
public void onNewResultImpl(EncodedImage newResult, @Status int status) {
  mProducerContext.getProducerListener().onProducerStart(mProducerContext, PRODUCER_NAME);
  // intermediate, null or uncacheable results are not cached, so we just forward them
  // as well as the images with unknown format which could be html response from the server
  if (isNotLast(status)
      || newResult == null
      || statusHasAnyFlag(status, DO_NOT_CACHE_ENCODED | IS_PARTIAL_RESULT)
      || newResult.getImageFormat() == ImageFormat.UNKNOWN) {
    mProducerContext
        .getProducerListener()
        .onProducerFinishWithSuccess(mProducerContext, PRODUCER_NAME, null);
    getConsumer().onNewResult(newResult, status);
    return;
  }

  final ImageRequest imageRequest = mProducerContext.getImageRequest();
  final CacheKey cacheKey =
      mCacheKeyFactory.getEncodedCacheKey(imageRequest, mProducerContext.getCallerContext());

  if (imageRequest.getCacheChoice() == ImageRequest.CacheChoice.SMALL) {
    mSmallImageBufferedDiskCache.put(cacheKey, newResult);
  } else {
    mDefaultBufferedDiskCache.put(cacheKey, newResult);
  }
  mProducerContext
      .getProducerListener()
      .onProducerFinishWithSuccess(mProducerContext, PRODUCER_NAME, null);

  getConsumer().onNewResult(newResult, status);
}
 
Example 3
Source File: ImagePipeline.java    From fresco with MIT License 5 votes vote down vote up
/**
 * Performs disk cache check synchronously. It is not recommended to use this unless you know what
 * exactly you are doing. Disk cache check is a costly operation, the call will block the caller
 * thread until the cache check is completed.
 *
 * @param imageRequest the imageRequest for the image to be looked up.
 * @return true if the image was found in the disk cache, false otherwise.
 */
public boolean isInDiskCacheSync(final ImageRequest imageRequest) {
  final CacheKey cacheKey = mCacheKeyFactory.getEncodedCacheKey(imageRequest, null);
  final ImageRequest.CacheChoice cacheChoice = imageRequest.getCacheChoice();

  switch (cacheChoice) {
    case DEFAULT:
      return mMainBufferedDiskCache.diskCheckSync(cacheKey);
    case SMALL:
      return mSmallImageBufferedDiskCache.diskCheckSync(cacheKey);
    default:
      return false;
  }
}
 
Example 4
Source File: EncodedProbeProducer.java    From fresco with MIT License 4 votes vote down vote up
@Override
public void onNewResultImpl(EncodedImage newResult, @Status int status) {
  try {
    if (FrescoSystrace.isTracing()) {
      FrescoSystrace.beginSection("EncodedProbeProducer#onNewResultImpl");
    }
    // intermediate, null or uncacheable results are not cached, so we just forward them
    // as well as the images with unknown format which could be html response from the server
    if (isNotLast(status)
        || newResult == null
        || statusHasAnyFlag(status, DO_NOT_CACHE_ENCODED | IS_PARTIAL_RESULT)
        || newResult.getImageFormat() == ImageFormat.UNKNOWN) {
      getConsumer().onNewResult(newResult, status);
      return;
    }

    final ImageRequest imageRequest = mProducerContext.getImageRequest();
    final CacheKey cacheKey =
        mCacheKeyFactory.getEncodedCacheKey(imageRequest, mProducerContext.getCallerContext());

    mEncodedMemoryCacheHistory.add(cacheKey);
    if (mProducerContext.getExtra(ProducerContext.ExtraKeys.ORIGIN).equals("memory_encoded")) {
      if (!mDiskCacheHistory.contains(cacheKey)) {
        final boolean isSmallRequest =
            (imageRequest.getCacheChoice() == ImageRequest.CacheChoice.SMALL);
        final BufferedDiskCache preferredCache =
            isSmallRequest ? mSmallImageBufferedDiskCache : mDefaultBufferedDiskCache;
        preferredCache.addKeyForAsyncProbing(cacheKey);
        mDiskCacheHistory.add(cacheKey);
      }
    } else {
      mDiskCacheHistory.add(cacheKey);
    }

    getConsumer().onNewResult(newResult, status);

  } finally {
    if (FrescoSystrace.isTracing()) {
      FrescoSystrace.endSection();
    }
  }
}
 
Example 5
Source File: BitmapProbeProducer.java    From fresco with MIT License 4 votes vote down vote up
@Override
public void onNewResultImpl(CloseableReference<CloseableImage> newResult, @Status int status) {
  try {
    if (FrescoSystrace.isTracing()) {
      FrescoSystrace.beginSection("BitmapProbeProducer#onNewResultImpl");
    }

    // intermediate or null are not cached, so we just forward them
    if (isNotLast(status) || newResult == null || statusHasAnyFlag(status, IS_PARTIAL_RESULT)) {
      getConsumer().onNewResult(newResult, status);
      return;
    }

    final ImageRequest imageRequest = mProducerContext.getImageRequest();
    final CacheKey cacheKey =
        mCacheKeyFactory.getEncodedCacheKey(imageRequest, mProducerContext.getCallerContext());
    if (mProducerContext.getExtra(ProducerContext.ExtraKeys.ORIGIN).equals("memory_bitmap")) {
      if (mProducerContext
              .getImagePipelineConfig()
              .getExperiments()
              .isEncodedMemoryCacheProbingEnabled()
          && !mEncodedMemoryCacheHistory.contains(cacheKey)) {
        mEncodedMemoryCache.probe(cacheKey);
        mEncodedMemoryCacheHistory.add(cacheKey);
      }
      if (mProducerContext.getImagePipelineConfig().getExperiments().isDiskCacheProbingEnabled()
          && !mDiskCacheHistory.contains(cacheKey)) {
        final boolean isSmallRequest =
            (imageRequest.getCacheChoice() == ImageRequest.CacheChoice.SMALL);
        final BufferedDiskCache preferredCache =
            isSmallRequest ? mSmallImageBufferedDiskCache : mDefaultBufferedDiskCache;
        preferredCache.addKeyForAsyncProbing(cacheKey);
        mDiskCacheHistory.add(cacheKey);
      }
    }

    getConsumer().onNewResult(newResult, status);

  } finally {
    if (FrescoSystrace.isTracing()) {
      FrescoSystrace.endSection();
    }
  }
}