preface

  • Glide, which is very powerfulAndroidImage loading open source framework I believe you are not unfamiliar

  • Because of its powerful function, its source code is very complex, which causes many people to be discouraged

  • I try to decompose the function of Glide, and separate for each function of the source code analysis, so as to reduce the complexity of Glide source code.

    Next, I will launch a series of Glide function source analysis, interested can continue to pay attention to

  • Today, I’m going to focus on the problem with using Glide cache: why the Glide cache doesn’t work. I hope you’ll enjoy it.

Please first read the article Android source analysis: Hand to hand with your analysis of Glide cache function


1. The background

  • GlideThe implementation of memory & disk caching is based on image cachingKeyUniquely identify
  • Developers tend to keep their images in the cloud to reduce costs & security


    Such as seven niuyun and so on.

  • In order to protect the customer’s picture resources, the picture cloud server will be in the pictureUrlAdd a token argument to the address
http://url.com/image.jpg?token=a6cvva6b02c670b0aCopy the code
  • GlideWhen loading the image, add is usedtokenPicture of parametersUrlAddress as

    idParameter to generate a cache Key

Problem 2.

  • For identificationtokenParameters may change, they are not set in stone
  • iftokenThe parameter changes, then the pictureUrlThen, the id parameter required to generate the cache key changes, that isThe cache Key also changes
  • This leads to the same picture, but becausetokenParameter changes, and the cache Key changes, so thatGlideThe cache function of the


    The cache Key changes, that is, the current cache Key of the same image is not the same as the Key previously written to the cache, which means that the previous cache cannot be found according to the current cache Key when reading from the cache, making it invalid


3. Solutions

3.1 the principle

Before generating the ID parameter of the cache Key, remove the token parameter from the image Url address with the token parameter, and then generate the ID parameter of the cache Key according to the initial image Url address

The id parameter of an image cache Key is always unique, which is equal to the image Url address

3.2 Reserve knowledge: logic for generating cache Key ID parameters

To generate cacheKeytheidParameter logic is: directly to the pictureURLAddress as the cache Keyidparameter

Look back at the article Android source code analysis: Hand in hand with your analysis of Glide cache function to generate cache Key code

public class Engine implements EngineJobListener, MemoryCache.ResourceRemovedListener, EngineResource.ResourceListener { public <T, Z, R> LoadStatus load(Key signature, int width, int height, DataFetcher<T> fetcher, DataLoadProvider<T, Z> loadProvider, Transformation<Z> transformation, ResourceTranscoder<Z, R> transcoder, Priority priority, boolean isMemoryCacheable, DiskCacheStrategy diskCacheStrategy, ResourceCallback cb) { Util.assertMainThread(); long startTime = LogTime.getLogTime(); final String id = fetcher.getId(); Fetcher = HttpUrlFetcher (); // Fetcher = HttpUrlFetcher (); Httpurlfetcher.getid () = keyFactory.buildKey(ID, signature, width, height, loadProvider.getCacheDecoder(),loadProvider.getSourceDecoder(), transformation, loadProvider.getEncoder(),transcoder, loadProvider.getSourceEncoder()); // Create a EngineKey object by passing the id to the factory method of the cache Key along with the signature, width, and height parameters. By overwriting equals() and hashCode(), the EngineKey is considered to be the same object only if all the EngineKey arguments passed in are the same... } <-- getId() --> public class HttpUrlFetcher implements DataFetcher<InputStream> {... private final GlideUrl glideUrl; // GlideUrl = when the image url is passed in the load() step, @override public String getId() {return glideurl.getcacheKey (); GetCacheKey () --> public class GlideUrl {private final URL URL; private final String stringUrl; . Public GlideUrl(URL URL) {this(URL, Headers); } public GlideUrl(String url) { this(url, Headers.DEFAULT); } public String getCacheKey() { return stringUrl ! = null ? stringUrl : url.toString(); // If you pass in a URL string (that is, the image address), return that string directly (most of the time). // If you pass in a URL object, return the result after toString(). }... }Copy the code

3.3 Implementation Scheme

We simply override getCacheKey() & remove the token argument from the image Url.

/** * create a subclass of GlideUrl & override getCacheKey() */ / 1. Public class mGlideUrl extends GlideUrl {private String mUrl; Public MyGlideUrl(String Url) {super(Url); mUrl = url; } @override public String getCacheKey() {return murl.replace (deleteToken(), ""); // Remove the token argument from the image Url with the token argument by deleteToken() // Return the original image Url with no token argument // ->> analysis 1} // analysis 1: deleteToken() private String deleteToken() { String tokenParam = ""; int tokenKeyIndex = mUrl.indexOf("? token=") >= 0 ? mUrl.indexOf("? token=") : mUrl.indexOf("&token="); if (tokenKeyIndex ! = -1) { int nextAndIndex = mUrl.indexOf("&", tokenKeyIndex + 1); if (nextAndIndex ! = -1) { tokenParam = mUrl.substring(tokenKeyIndex + 1, nextAndIndex + 1); } else { tokenParam = mUrl.substring(tokenKeyIndex); } } return tokenParam; */ Glide. With (this).load(new mGlideUrl(url)).into(imageView); // note: a. If you pass in the image url as before, the original GlideUrl class will be used internally. That is, the url of the incoming image is directly used as the Id parameter of the cache key without any processing for the token parameterCopy the code

4. To summarize

  • This paper mainly explains the use of Glide’s picture cache function
  • Android source code analysis: this is a detailed picture loading library Glide source code explain walkthrough Android picture loading library: the most comprehensive analysis of Glide usage

  • Next, I will continue to source analysis of Glide’s other features. If you are interested, you can continue to pay attention to Carson_Ho’s Android development notes


Thumbs up for top/comment! Because your encouragement is the biggest power that I write!