Skip to content

Commit 2da616f

Browse files
oprisnikmeta-codesync[bot]
authored andcommitted
Added option to specify custom cache key to ImageRequest and ImageRequestBuilder
Differential Revision: D91033614 fbshipit-source-id: b3e7e2ac2ca9750069e4d730e051df2bdfbd7810
1 parent bd39932 commit 2da616f

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

imagepipeline/src/main/java/com/facebook/imagepipeline/cache/DefaultCacheKeyFactory.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,13 @@ public static synchronized DefaultCacheKeyFactory getInstance() {
3232

3333
@Override
3434
public CacheKey getBitmapCacheKey(ImageRequest request, @Nullable Object callerContext) {
35+
String sourceString = request.getCustomCacheKey();
36+
if (sourceString == null) {
37+
sourceString = getCacheKeySourceUri(request.getSourceUri()).toString();
38+
}
3539
BitmapMemoryCacheKey cacheKey =
3640
new BitmapMemoryCacheKey(
37-
getCacheKeySourceUri(request.getSourceUri()).toString(),
41+
sourceString,
3842
request.getResizeOptions(),
3943
request.getRotationOptions(),
4044
request.getImageDecodeOptions(),
@@ -56,9 +60,13 @@ public CacheKey getPostprocessedBitmapCacheKey(
5660
postprocessorCacheKey = null;
5761
postprocessorName = null;
5862
}
63+
String sourceString = request.getCustomCacheKey();
64+
if (sourceString == null) {
65+
sourceString = getCacheKeySourceUri(request.getSourceUri()).toString();
66+
}
5967
BitmapMemoryCacheKey cacheKey =
6068
new BitmapMemoryCacheKey(
61-
getCacheKeySourceUri(request.getSourceUri()).toString(),
69+
sourceString,
6270
request.getResizeOptions(),
6371
request.getRotationOptions(),
6472
request.getImageDecodeOptions(),
@@ -75,6 +83,10 @@ public CacheKey getEncodedCacheKey(ImageRequest request, @Nullable Object caller
7583
@Override
7684
public CacheKey getEncodedCacheKey(
7785
ImageRequest request, Uri sourceUri, @Nullable Object callerContext) {
86+
String customCacheKey = request.getCustomCacheKey();
87+
if (customCacheKey != null) {
88+
return new SimpleCacheKey(customCacheKey);
89+
}
7890
return new SimpleCacheKey(getCacheKeySourceUri(sourceUri).toString());
7991
}
8092

imagepipeline/src/main/java/com/facebook/imagepipeline/request/ImageRequest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ public class ImageRequest {
132132

133133
private final @Nullable String mDiskCacheId;
134134

135+
private final @Nullable String mCustomCacheKey;
136+
135137
private final int mDelayMs;
136138

137139
/** Whether to extract first frame thumbnail from video. */
@@ -196,6 +198,8 @@ protected ImageRequest(ImageRequestBuilder builder) {
196198

197199
mDiskCacheId = builder.getDiskCacheId();
198200

201+
mCustomCacheKey = builder.getCustomCacheKey();
202+
199203
mIsFirstFrameThumbnailEnabled = builder.getIsFirstFrameThumbnailEnabled();
200204
}
201205

@@ -325,6 +329,10 @@ public synchronized File getSourceFile() {
325329
return mDiskCacheId;
326330
}
327331

332+
public @Nullable String getCustomCacheKey() {
333+
return mCustomCacheKey;
334+
}
335+
328336
public Boolean isFirstFrameThumbnailEnabled() {
329337
return mIsFirstFrameThumbnailEnabled;
330338
}
@@ -348,6 +356,7 @@ public boolean equals(@Nullable Object o) {
348356
if (!Objects.equal(mSourceUri, request.mSourceUri)
349357
|| !Objects.equal(mCacheChoice, request.mCacheChoice)
350358
|| !Objects.equal(mDiskCacheId, request.mDiskCacheId)
359+
|| !Objects.equal(mCustomCacheKey, request.mCustomCacheKey)
351360
|| !Objects.equal(mSourceFile, request.mSourceFile)
352361
|| !Objects.equal(mBytesRange, request.mBytesRange)
353362
|| !Objects.equal(mImageDecodeOptions, request.mImageDecodeOptions)
@@ -397,6 +406,7 @@ public int hashCode() {
397406
result = HashCode.extend(result, postprocessorCacheKey);
398407
result = HashCode.extend(result, mResizingAllowedOverride);
399408
result = HashCode.extend(result, mDownsampleOverride);
409+
result = HashCode.extend(result, mCustomCacheKey);
400410
result = HashCode.extend(result, mDelayMs);
401411
result = HashCode.extend(result, mLoadThumbnailOnly);
402412
result = HashCode.extend(result, mIsFirstFrameThumbnailEnabled);
@@ -429,6 +439,7 @@ public String toString() {
429439
.add("isDiskCacheEnabled", mIsDiskCacheEnabled)
430440
.add("isMemoryCacheEnabled", mIsMemoryCacheEnabled)
431441
.add("decodePrefetches", mDecodePrefetches)
442+
.add("customCacheKey", mCustomCacheKey)
432443
.add("delayMs", mDelayMs)
433444
.add("isFirstFrameThumbnailEnabled", mIsFirstFrameThumbnailEnabled)
434445
.toString();

imagepipeline/src/main/java/com/facebook/imagepipeline/request/ImageRequestBuilder.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public class ImageRequestBuilder {
5353
private @Nullable DownsampleMode mDownsampleOverride = null;
5454
private int mDelayMs;
5555
private @Nullable String mDiskCacheId = null;
56+
private @Nullable String mCustomCacheKey = null;
5657
private Boolean mIsFirstFrameThumbnailEnabled = false;
5758

5859
/**
@@ -110,6 +111,7 @@ public static ImageRequestBuilder fromRequest(ImageRequest imageRequest) {
110111
.setShouldDecodePrefetches(imageRequest.shouldDecodePrefetches())
111112
.setDelayMs(imageRequest.getDelayMs())
112113
.setDiskCacheId(imageRequest.getDiskCacheId())
114+
.setCustomCacheKey(imageRequest.getCustomCacheKey())
113115
.setDownsampleOverride(imageRequest.getDownsampleOverride())
114116
.setResizingAllowedOverride(imageRequest.getResizingAllowedOverride())
115117
.setIsFirstFrameThumbnailEnabled(imageRequest.isFirstFrameThumbnailEnabled())
@@ -454,6 +456,25 @@ public ImageRequestBuilder setDiskCacheId(@Nullable String diskCacheId) {
454456
return mDiskCacheId;
455457
}
456458

459+
/**
460+
* Sets a custom cache key for this request. When set, this key will be used instead of deriving
461+
* the cache key from the source URI.
462+
*
463+
* @param customCacheKey a custom cache key to use for caching this image.
464+
* @return the modified builder instance
465+
*/
466+
public ImageRequestBuilder setCustomCacheKey(@Nullable String customCacheKey) {
467+
this.mCustomCacheKey = customCacheKey;
468+
return this;
469+
}
470+
471+
/**
472+
* @return the custom cache key to use for caching this image.
473+
*/
474+
public @Nullable String getCustomCacheKey() {
475+
return mCustomCacheKey;
476+
}
477+
457478
/**
458479
* Builds the Request.
459480
*

0 commit comments

Comments
 (0)