import com.facebook.imagepipeline.image.EncodedImage; //导入方法依赖的package包/类
/**
* Associates encodedImage with given key in disk cache. Disk write is performed on background
* thread, so the caller of this method is not blocked
*/
public void put(
final CacheKey key,
EncodedImage encodedImage) {
Preconditions.checkNotNull(key);
Preconditions.checkArgument(EncodedImage.isValid(encodedImage));
// Store encodedImage in staging area
mStagingArea.put(key, encodedImage);
// Write to disk cache. This will be executed on background thread, so increment the ref count.
// When this write completes (with success/failure), then we will bump down the ref count
// again.
final EncodedImage finalEncodedImage = EncodedImage.cloneOrNull(encodedImage);
try {
mWriteExecutor.execute(
new Runnable() {
@Override
public void run() {
try {
writeToDiskCache(key, finalEncodedImage);
} finally {
mStagingArea.remove(key, finalEncodedImage);
EncodedImage.closeSafely(finalEncodedImage);
}
}
});
} catch (Exception exception) {
// We failed to enqueue cache write. Log failure and decrement ref count
// TODO: 3697790
FLog.w(
TAG,
exception,
"Failed to schedule disk-cache write for %s",
key.getUriString());
mStagingArea.remove(key, encodedImage);
EncodedImage.closeSafely(finalEncodedImage);
}
}