Preface
-
Glide
, This function is very powerfulAndroid
image loading believe we are not familiar open-source framework
-
Because of its powerful functions, its source code is very complicated, which causes many people to stay away.
-
Try to
Glide
function decomposition, and source code analysis separately for each function, thereby reducing theGlide
complexity of the source code.Next, I will launch a series on
Glide
functional analysis of the source code, are interested can continue to focus on -
Today, I will mainly explain
Glide
the problem when using the cache function: why Glide's cache does not work, I hope you will like it.
Please read the article Android source code analysis: take you to analyze the cache function of Glide first
1. Background
Glide
The realization of memory & disk caching is basedKey
on the unique identification of the image cache- In order to reduce costs and security, developers often store pictures on cloud servers
Such as Qiniuyun and so on.
- In order to protect the customer's picture resources, the picture cloud server will
Url
add a token parameter to the picture address
http://url.com/image.jpg?token=a6cvva6b02c670b0a
Glide
When loading the picture will be used plus thetoken
parameters of the pictureUrl
address as
id
parameters to generate the cache Key
2. Problem
- As the
token
parameters of identity authentication may change, not static - If
token
parameters have changed, the pictureUrl
change along, then generate the required id parameter cache key changes that cache Key changes will follow - This led to the same picture, but because of
token
parameter changes, which led to a change in Key cache, so that theGlide
cache function failure
The cache key has changed, that is, the current cache key of the same picture is different from the key previously written to the cache, which means that the previous cache cannot be found based on the current cache key when reading the cache, thus making it invalid
3. Solution
3.1 Principle
In generating the cache Key
before id parameter, with the token
picture parameter of Url
the address remove token
parameters to the initial images Url
to generate the cache address Key
id parameter
Key
The id parameter of the cache of a picture is always unique, which is equal to the pictureUrl
address
3.2 Reserve knowledge: the logic of generating the id parameter of the cache key
Generating a cache Key
of id
logical parameters are: the picture directly to the URL
address buffer as a Key id
Parameter
Look back at the article Android source code analysis: take you to analyze Glide's 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();
// id
// id = url
//fetcher = HttpUrlFetcher HttpUrlFetcher.getid ->> 19
EngineKey key = keyFactory.buildKey(id, signature, width, height, loadProvider.getCacheDecoder(),loadProvider.getSourceDecoder(), transformation, loadProvider.getEncoder(),transcoder, loadProvider.getSourceEncoder());
// id signature width height 10 Key EngineKey
// equals() hashCode() EngineKey EngineKey
// EngineKey Glide Key
...
}
<-- 19 getId() -->
public class HttpUrlFetcher implements DataFetcher<InputStream> {
...
private final GlideUrl glideUrl;
//GlideUrl = 2 load() url Glide url GlideUrl
@Override
public String getId() {
return glideUrl.getCacheKey();
//->> 20
}
<-- 20 getCacheKey() -->
public class GlideUrl {
private final URL url;
private final String stringUrl;
...
//GlideUrl
public GlideUrl(URL url) {
this(url, Headers.DEFAULT);
}
public GlideUrl(String url) {
this(url, Headers.DEFAULT);
}
public String getCacheKey() {
return stringUrl != null ? stringUrl : url.toString();
// GlideUrl
// URL
// URL toString()
}
...
}
3.3 Implementation scheme
That we simply rewrite getCacheKey()
& the pictures with a token parameter Url
address removed token parameter.
/**
* GlideUrl & getCacheKey()
**/
//1. GlideUrl
public class mGlideUrl extends GlideUrl {
private String mUrl;
// token Url
public MyGlideUrl(String url) {
super(url);
mUrl = url;
}
//2. getCacheKey()
@Override
public String getCacheKey() {
return mUrl.replace(deleteToken(), "");
// deleteToken() token Url token
// token URL
//->> 1
}
// 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;
}
}
/**
* load() mGlideUrl
**/
Glide.with(this)
.load(new mGlideUrl(url))
.into(imageView);
// a. url GlideUrl
// b. url key Id token
4. Summary
- This article mainly explains the use of
Glide
the image caching function -
For related articles on Glide, read
Android source code analysis: take you to analyze Glide's cache function
Android source code analysis: this is a detailed picture loading library Glide source code explanation strategy
Android image loading library: the most comprehensive analysis of Glide usage -
Now I will continue on
Glide
other functions are source code analysis , are interested can continue to focus on Android development Carson_Ho notes