preface
-
Glide, the function is very powerful Android picture loading open source framework I believe we 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 will focus on the use of
Glide
Problems with the caching function: hope you will enjoy why Glide’s cache does not work.
Please read the article first:
- Android source code analysis: Hand – in – hand take you to understand Glide cache mechanism
- Android: this is a comprehensive & detailed picture loading library Glide source analysis
1. The background
Glide
The implementation of memory & disk caching is based on image cachingKey
Uniquely 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 picture
Url
Add a token argument to the address
http://url.com/image.jpg?token=a6cvva6b02c670b0a
Copy the code
Glide
When loading the image, add is usedtoken
Picture of parametersUrl
Address asid
Parameter to generate a cache Key
Problem 2.
- For identification
token
Parameters may change, they are not set in stone - if
token
The parameter changes, then the pictureUrl
Then, the id parameter required to generate the cache key changes, that isThe cache Key also changes - This leads to the same picture, but because
token
Parameter changes, and the cache Key changes, so thatGlide
The 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
The logic of generating the ID parameter of the cache Key is as follows: The URL address of the image is directly used as the ID parameter of the cache Key
Please refer back to the article generated cache Key code: Android source code analysis: Hand in hand take you into depth understanding of Glide cache mechanism
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 overwriting equals() and equals()hashCode(), which ensures that the EngineKey object is the same only if all the parameters of the EngineKey are the same. } <-- getId() --> public class HttpUrlFetcher implements DataFetcher<InputStream> {... private final GlideUrl glideUrl; GlideUrl = When the image URL is passed to load(), Glide wraps it internally into a GlideUrl object @override public StringgetId() {
returnglideUrl.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 StringgetCacheKey() {
returnstringUrl ! = 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; } // 2. Override getCacheKey() @override public StringgetCacheKey() {
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 StringdeleteToken() {
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); }}returntokenParam; */ 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
- Read relevant articles about Glide
- Android source code analysis: Hand – in – hand take you to understand Glide cache mechanism
- Android: this is a comprehensive & detailed picture loading library Glide source analysis
- 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