use of com.facebook.cache.common.CacheKey in project fresco by facebook.
the class MediaVariationsFallbackProducer method attemptCacheReadForVariant.
private Task attemptCacheReadForVariant(final Consumer<EncodedImage> consumer, final ProducerContext producerContext, ImageRequest imageRequest, MediaVariations mediaVariations, List<MediaVariations.Variant> sortedVariants, int index, AtomicBoolean isCancelled) {
final MediaVariations.Variant variant = sortedVariants.get(index);
final CacheKey cacheKey = mCacheKeyFactory.getEncodedCacheKey(imageRequest, variant.getUri(), producerContext.getCallerContext());
final ImageRequest.CacheChoice cacheChoice;
if (variant.getCacheChoice() == null) {
cacheChoice = imageRequest.getCacheChoice();
} else {
cacheChoice = variant.getCacheChoice();
}
final BufferedDiskCache preferredCache = cacheChoice == ImageRequest.CacheChoice.SMALL ? mSmallImageBufferedDiskCache : mDefaultBufferedDiskCache;
Task<EncodedImage> readTask = preferredCache.get(cacheKey, isCancelled);
Continuation<EncodedImage, Void> continuation = onFinishDiskReads(consumer, producerContext, imageRequest, mediaVariations, sortedVariants, index, isCancelled);
return readTask.continueWith(continuation);
}
use of com.facebook.cache.common.CacheKey in project fresco by facebook.
the class PostprocessedBitmapMemoryCacheProducer method produceResults.
@Override
public void produceResults(final Consumer<CloseableReference<CloseableImage>> consumer, final ProducerContext producerContext) {
final ProducerListener listener = producerContext.getListener();
final String requestId = producerContext.getId();
final ImageRequest imageRequest = producerContext.getImageRequest();
final Object callerContext = producerContext.getCallerContext();
// If there's no postprocessor or the postprocessor doesn't require caching, forward results.
final Postprocessor postprocessor = imageRequest.getPostprocessor();
if (postprocessor == null || postprocessor.getPostprocessorCacheKey() == null) {
mInputProducer.produceResults(consumer, producerContext);
return;
}
listener.onProducerStart(requestId, getProducerName());
final CacheKey cacheKey = mCacheKeyFactory.getPostprocessedBitmapCacheKey(imageRequest, callerContext);
CloseableReference<CloseableImage> cachedReference = mMemoryCache.get(cacheKey);
if (cachedReference != null) {
listener.onProducerFinishWithSuccess(requestId, getProducerName(), listener.requiresExtraMap(requestId) ? ImmutableMap.of(VALUE_FOUND, "true") : null);
consumer.onProgressUpdate(1.0f);
consumer.onNewResult(cachedReference, true);
cachedReference.close();
} else {
final boolean isRepeatedProcessor = postprocessor instanceof RepeatedPostprocessor;
Consumer<CloseableReference<CloseableImage>> cachedConsumer = new CachedPostprocessorConsumer(consumer, cacheKey, isRepeatedProcessor, mMemoryCache);
listener.onProducerFinishWithSuccess(requestId, getProducerName(), listener.requiresExtraMap(requestId) ? ImmutableMap.of(VALUE_FOUND, "false") : null);
mInputProducer.produceResults(cachedConsumer, producerContext);
}
}
use of com.facebook.cache.common.CacheKey in project fresco by facebook.
the class BitmapMemoryCacheProducer method produceResults.
@Override
public void produceResults(final Consumer<CloseableReference<CloseableImage>> consumer, final ProducerContext producerContext) {
final ProducerListener listener = producerContext.getListener();
final String requestId = producerContext.getId();
listener.onProducerStart(requestId, getProducerName());
final ImageRequest imageRequest = producerContext.getImageRequest();
final Object callerContext = producerContext.getCallerContext();
final CacheKey cacheKey = mCacheKeyFactory.getBitmapCacheKey(imageRequest, callerContext);
CloseableReference<CloseableImage> cachedReference = mMemoryCache.get(cacheKey);
if (cachedReference != null) {
boolean isFinal = cachedReference.get().getQualityInfo().isOfFullQuality();
if (isFinal) {
listener.onProducerFinishWithSuccess(requestId, getProducerName(), listener.requiresExtraMap(requestId) ? ImmutableMap.of(EXTRA_CACHED_VALUE_FOUND, "true") : null);
consumer.onProgressUpdate(1f);
}
consumer.onNewResult(cachedReference, isFinal);
cachedReference.close();
if (isFinal) {
return;
}
}
if (producerContext.getLowestPermittedRequestLevel().getValue() >= ImageRequest.RequestLevel.BITMAP_MEMORY_CACHE.getValue()) {
listener.onProducerFinishWithSuccess(requestId, getProducerName(), listener.requiresExtraMap(requestId) ? ImmutableMap.of(EXTRA_CACHED_VALUE_FOUND, "false") : null);
consumer.onNewResult(null, true);
return;
}
Consumer<CloseableReference<CloseableImage>> wrappedConsumer = wrapConsumer(consumer, cacheKey);
listener.onProducerFinishWithSuccess(requestId, getProducerName(), listener.requiresExtraMap(requestId) ? ImmutableMap.of(EXTRA_CACHED_VALUE_FOUND, "false") : null);
mInputProducer.produceResults(wrappedConsumer, producerContext);
}
use of com.facebook.cache.common.CacheKey in project fresco by facebook.
the class ImagePipeline method isInBitmapMemoryCache.
/**
* Returns whether the image is stored in the bitmap memory cache.
*
* @param imageRequest the imageRequest for the image to be looked up.
* @return true if the image was found in the bitmap memory cache, false otherwise.
*/
public boolean isInBitmapMemoryCache(final ImageRequest imageRequest) {
if (imageRequest == null) {
return false;
}
final CacheKey cacheKey = mCacheKeyFactory.getBitmapCacheKey(imageRequest, null);
CloseableReference<CloseableImage> ref = mBitmapMemoryCache.get(cacheKey);
try {
return CloseableReference.isValid(ref);
} finally {
CloseableReference.closeSafely(ref);
}
}
use of com.facebook.cache.common.CacheKey in project fresco by facebook.
the class ImagePipelineTest method testClearMemoryCaches.
@Test
public void testClearMemoryCaches() {
String uriString = "http://dummy/string";
Uri uri = Uri.parse(uriString);
CacheKey dummyCacheKey = mock(CacheKey.class);
mImagePipeline.clearMemoryCaches();
ArgumentCaptor<Predicate> bitmapCachePredicateCaptor = ArgumentCaptor.forClass(Predicate.class);
verify(mBitmapMemoryCache).removeAll(bitmapCachePredicateCaptor.capture());
Predicate<CacheKey> bitmapMemoryCacheKeyPredicate = bitmapCachePredicateCaptor.getValue();
BitmapMemoryCacheKey bitmapMemoryCacheKey1 = mock(BitmapMemoryCacheKey.class);
BitmapMemoryCacheKey bitmapMemoryCacheKey2 = mock(BitmapMemoryCacheKey.class);
when(bitmapMemoryCacheKey1.containsUri(uri)).thenReturn(true);
when(bitmapMemoryCacheKey2.containsUri(uri)).thenReturn(false);
assertTrue(bitmapMemoryCacheKeyPredicate.apply(bitmapMemoryCacheKey1));
assertTrue(bitmapMemoryCacheKeyPredicate.apply(bitmapMemoryCacheKey2));
assertTrue(bitmapMemoryCacheKeyPredicate.apply(dummyCacheKey));
ArgumentCaptor<Predicate> encodedMemoryCachePredicateCaptor = ArgumentCaptor.forClass(Predicate.class);
verify(mEncodedMemoryCache).removeAll(encodedMemoryCachePredicateCaptor.capture());
Predicate<CacheKey> encodedMemoryCacheKeyPredicate = encodedMemoryCachePredicateCaptor.getValue();
SimpleCacheKey simpleCacheKey1 = new SimpleCacheKey(uriString);
SimpleCacheKey simpleCacheKey2 = new SimpleCacheKey("rubbish");
assertTrue(encodedMemoryCacheKeyPredicate.apply(simpleCacheKey1));
assertTrue(encodedMemoryCacheKeyPredicate.apply(simpleCacheKey2));
assertTrue(encodedMemoryCacheKeyPredicate.apply(dummyCacheKey));
}
Aggregations