Skip to content

Commit

Permalink
Add callercontext to BitmapCacheKey
Browse files Browse the repository at this point in the history
Reviewed By: massimocarli

Differential Revision: D3264114

fbshipit-source-id: 1b4bbab202d061c04f05960b1427e5eaa78ea2b8
  • Loading branch information
aagnes-zz authored and Facebook Github Bot 1 committed May 12, 2016
1 parent 6b58e66 commit 1def030
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,16 @@ public class BitmapMemoryCacheKey implements CacheKey {
private final @Nullable CacheKey mPostprocessorCacheKey;
private final @Nullable String mPostprocessorName;
private final int mHash;
private final Object mCallerContext;

public BitmapMemoryCacheKey(
String sourceString,
@Nullable ResizeOptions resizeOptions,
boolean autoRotated,
ImageDecodeOptions imageDecodeOptions,
@Nullable CacheKey postprocessorCacheKey,
@Nullable String postprocessorName) {
@Nullable String postprocessorName,
Object callerContext) {
mSourceString = Preconditions.checkNotNull(sourceString);
mResizeOptions = resizeOptions;
mAutoRotated = autoRotated;
Expand All @@ -57,6 +59,7 @@ public BitmapMemoryCacheKey(
mImageDecodeOptions,
mPostprocessorCacheKey,
postprocessorName);
mCallerContext = callerContext;
}

@Override
Expand Down Expand Up @@ -106,4 +109,8 @@ public String toString() {
mPostprocessorName,
mHash);
}

public Object getCallerContext() {
return mCallerContext;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@

package com.facebook.imagepipeline.cache;

import java.util.List;

import android.net.Uri;

import com.facebook.cache.common.CacheKey;
import com.facebook.imagepipeline.request.ImageRequest;

Expand All @@ -24,12 +20,12 @@ public interface CacheKeyFactory {
/**
* @return {@link CacheKey} for doing bitmap cache lookups in the pipeline.
*/
CacheKey getBitmapCacheKey(ImageRequest request);
CacheKey getBitmapCacheKey(ImageRequest request, Object callerContext);

/**
* @return {@link CacheKey} for doing post-processed bitmap cache lookups in the pipeline.
*/
CacheKey getPostprocessedBitmapCacheKey(ImageRequest request);
CacheKey getPostprocessedBitmapCacheKey(ImageRequest request, Object callerContext);

/**
* @return {@link CacheKey} for doing encoded image lookups in the pipeline.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@

package com.facebook.imagepipeline.cache;

import java.util.ArrayList;
import java.util.List;

import android.net.Uri;

import com.facebook.cache.common.CacheKey;
Expand All @@ -37,18 +34,19 @@ public static synchronized DefaultCacheKeyFactory getInstance() {
}

@Override
public CacheKey getBitmapCacheKey(ImageRequest request) {
public CacheKey getBitmapCacheKey(ImageRequest request, Object callerContext) {
return new BitmapMemoryCacheKey(
getCacheKeySourceUri(request.getSourceUri()).toString(),
request.getResizeOptions(),
request.getAutoRotateEnabled(),
request.getImageDecodeOptions(),
null,
null);
null,
callerContext);
}

@Override
public CacheKey getPostprocessedBitmapCacheKey(ImageRequest request) {
public CacheKey getPostprocessedBitmapCacheKey(ImageRequest request, Object callerContext) {
final Postprocessor postprocessor = request.getPostprocessor();
final CacheKey postprocessorCacheKey;
final String postprocessorName;
Expand All @@ -65,7 +63,8 @@ public CacheKey getPostprocessedBitmapCacheKey(ImageRequest request) {
request.getAutoRotateEnabled(),
request.getImageDecodeOptions(),
postprocessorCacheKey,
postprocessorName);
postprocessorName,
callerContext);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

import javax.annotation.concurrent.ThreadSafe;

import java.lang.Exception;
import java.util.Set;
import java.util.concurrent.CancellationException;
import java.util.concurrent.atomic.AtomicLong;
Expand All @@ -28,26 +27,24 @@
import com.facebook.datasource.DataSources;
import com.facebook.datasource.SimpleDataSource;
import com.facebook.imagepipeline.cache.BufferedDiskCache;
import com.facebook.imagepipeline.cache.MemoryCache;
import com.facebook.imagepipeline.cache.CacheKeyFactory;
import com.facebook.imagepipeline.cache.MemoryCache;
import com.facebook.imagepipeline.common.Priority;
import com.facebook.imagepipeline.datasource.CloseableProducerToDataSourceAdapter;
import com.facebook.imagepipeline.datasource.ProducerToDataSourceAdapter;
import com.facebook.imagepipeline.image.CloseableImage;
import com.facebook.imagepipeline.listener.ForwardingRequestListener;
import com.facebook.imagepipeline.listener.RequestListener;
import com.facebook.imagepipeline.memory.PooledByteBuffer;
import com.facebook.imagepipeline.producers.Producer;
import com.facebook.imagepipeline.producers.SettableProducerContext;
import com.facebook.imagepipeline.listener.ForwardingRequestListener;
import com.facebook.imagepipeline.producers.ThreadHandoffProducerQueue;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.listener.RequestListener;

import bolts.Continuation;
import com.android.internal.util.Predicate;

import com.facebook.imagepipeline.request.ImageRequestBuilder;

import bolts.Continuation;
import bolts.Task;
import com.facebook.imagepipeline.producers.ThreadHandoffProducerQueue;
import com.android.internal.util.Predicate;

/**
* The entry point for the image pipeline.
Expand Down Expand Up @@ -395,7 +392,7 @@ public boolean isInBitmapMemoryCache(final Uri uri) {
* @return true if the image was found in the bitmap memory cache, false otherwise.
*/
public boolean isInBitmapMemoryCache(final ImageRequest imageRequest) {
final CacheKey cacheKey = mCacheKeyFactory.getBitmapCacheKey(imageRequest);
final CacheKey cacheKey = mCacheKeyFactory.getBitmapCacheKey(imageRequest, null);
CloseableReference<CloseableImage> ref = mBitmapMemoryCache.get(cacheKey);
try {
return CloseableReference.isValid(ref);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ public BitmapMemoryCacheKeyMultiplexProducer(
protected Pair<CacheKey, ImageRequest.RequestLevel> getKey(
ProducerContext producerContext) {
return Pair.create(
mCacheKeyFactory.getBitmapCacheKey(producerContext.getImageRequest()),
mCacheKeyFactory.getBitmapCacheKey(
producerContext.getImageRequest(),
producerContext.getCallerContext()),
producerContext.getLowestPermittedRequestLevel());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ public void produceResults(
final String requestId = producerContext.getId();
listener.onProducerStart(requestId, getProducerName());
final ImageRequest imageRequest = producerContext.getImageRequest();
final CacheKey cacheKey = mCacheKeyFactory.getBitmapCacheKey(imageRequest);
final Object callerContext = producerContext.getCallerContext();
final CacheKey cacheKey = mCacheKeyFactory.getBitmapCacheKey(imageRequest, callerContext);

CloseableReference<CloseableImage> cachedReference = mMemoryCache.get(cacheKey);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public void produceResults(
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();
Expand All @@ -58,7 +59,8 @@ public void produceResults(
return;
}
listener.onProducerStart(requestId, getProducerName());
final CacheKey cacheKey = mCacheKeyFactory.getPostprocessedBitmapCacheKey(imageRequest);
final CacheKey cacheKey =
mCacheKeyFactory.getPostprocessedBitmapCacheKey(imageRequest, callerContext);
CloseableReference<CloseableImage> cachedReference = mMemoryCache.get(cacheKey);
if (cachedReference != null) {
listener.onProducerFinishWithSuccess(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public class BitmapMemoryCacheGetProducerTest {
@Mock public ProducerListener mProducerListener;
@Mock public Exception mException;
@Mock public BitmapMemoryCacheKey mCacheKey;
@Mock public Object mCallerContext;
private final String mRequestId = "mRequestId";
private CloseableImage mCloseableImage1;
private CloseableReference<CloseableImage> mFinalImageReference;
Expand All @@ -75,7 +76,9 @@ public void setUp() {
when(mProducerListener.requiresExtraMap(mRequestId)).thenReturn(true);
when(mProducerContext.getLowestPermittedRequestLevel())
.thenReturn(ImageRequest.RequestLevel.FULL_FETCH);
when(mCacheKeyFactory.getBitmapCacheKey(mImageRequest)).thenReturn(mCacheKey);
when(mProducerContext.getCallerContext())
.thenReturn(PRODUCER_NAME);
when(mCacheKeyFactory.getBitmapCacheKey(mImageRequest, PRODUCER_NAME)).thenReturn(mCacheKey);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,10 @@ public void setUp() {
when(mProducerContext.getId()).thenReturn(mRequestId);
when(mProducerContext.getLowestPermittedRequestLevel())
.thenReturn(ImageRequest.RequestLevel.FULL_FETCH);
when(mProducerContext.getCallerContext()).thenReturn(PRODUCER_NAME);
when(mProducerListener.requiresExtraMap(mRequestId)).thenReturn(true);
when(mCacheKeyFactory.getBitmapCacheKey(mImageRequest)).thenReturn(mBitmapMemoryCacheKey);
when(mCacheKeyFactory.getBitmapCacheKey(mImageRequest, PRODUCER_NAME))
.thenReturn(mBitmapMemoryCacheKey);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,21 @@
import org.mockito.*;
import org.mockito.invocation.*;
import org.mockito.stubbing.*;
import org.robolectric.*;
import org.robolectric.annotation.*;

import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertTrue;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;

/**
* Checks basic properties of the multiplex producer, that is:
Expand Down Expand Up @@ -122,8 +128,10 @@ public void setUp() {
mIntermediateImageReference1 = CloseableReference.of(mIntermediateCloseableImage1);
mIntermediateImageReference2 = CloseableReference.of(mIntermediateCloseableImage2);

when(mCacheKeyFactory.getBitmapCacheKey(mImageRequest1)).thenReturn(mBitmapMemoryCacheKey1);
when(mCacheKeyFactory.getBitmapCacheKey(mImageRequest2)).thenReturn(mBitmapMemoryCacheKey2);
when(mCacheKeyFactory.getBitmapCacheKey(mImageRequest1, mCallerContext))
.thenReturn(mBitmapMemoryCacheKey1);
when(mCacheKeyFactory.getBitmapCacheKey(mImageRequest2, mCallerContext))
.thenReturn(mBitmapMemoryCacheKey2);

doAnswer(
new Answer() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,11 @@ public void setUp() {
when(mProducerContext.getListener()).thenReturn(mProducerListener);
when(mProducerListener.requiresExtraMap(mRequestId)).thenReturn(true);
when(mProducerContext.getId()).thenReturn(mRequestId);
when(mProducerContext.getCallerContext()).thenReturn(PRODUCER_NAME);
when(mImageRequest.getPostprocessor()).thenReturn(mPostprocessor);
when(mPostprocessor.getPostprocessorCacheKey()).thenReturn(mPostProcessorCacheKey);
when(mRepeatedPostprocessor.getPostprocessorCacheKey()).thenReturn(mPostProcessorCacheKey);
when(mCacheKeyFactory.getPostprocessedBitmapCacheKey(mImageRequest))
when(mCacheKeyFactory.getPostprocessedBitmapCacheKey(mImageRequest, PRODUCER_NAME))
.thenReturn(mPostprocessedBitmapCacheKey);
}

Expand Down

0 comments on commit 1def030

Please sign in to comment.