浅谈Glide缓存key的问题

作者:Dynamic_2018 时间:2023-10-05 05:26:35 

最近项目里面有个地方是在前面用glide加载图片后,后面再另外一个地方加载相同图片时没有复用glide的缓存,而是自己另外又重新缓存了一套。

查找后发现问题是glide缓存的key不一致的问题。

从key的生成可以看到和很多参数有关,逐一排查后,发现了width和height还有id不一样。这3个是项目外面传进来的。


EngineKey key = keyFactory.buildKey(id, signature, width, height, loadProvider.getCacheDecoder(),
       loadProvider.getSourceDecoder(), transformation, loadProvider.getEncoder(),
       transcoder, loadProvider.getSourceEncoder());

key的作用大概是通过下面三步里面去找数据

如果都为null,就会进入函数最后边的开线程去decode(相当于缓存没找到,准备重新加载数据吧)


   EngineJob engineJob = engineJobFactory.build(key, isMemoryCacheable);
   DecodeJob<T, Z, R> decodeJob = new DecodeJob<T, Z, R>(key, width, height, fetcher, loadProvider, transformation,
       transcoder, diskCacheProvider, diskCacheStrategy, priority);
   EngineRunnable runnable = new EngineRunnable(engineJob, decodeJob, priority);
   jobs.put(key, engineJob);
   engineJob.addCallback(cb);
   engineJob.start(runnable);

进入EngineRunnable的run方法看


resource = decode();
private Resource<?> decode() throws Exception {
   if (isDecodingFromCache()) {
     return decodeFromCache();
   } else {
     return decodeFromSource();
   }
 }

其中loadCache还是loadFromSource的条件


 private boolean isDecodingFromCache() {
   return stage == Stage.CACHE;
 }

默认stage会进去,走到decodeFromCache(),由于cache里没有,返回null到run方法里面触发加载失败的回调


if (resource == null) {
     onLoadFailed(exception);
   } else {
     onLoadComplete(resource);
   }

在回调中重新提交一个runnable,改变stage,下一次run执行时,stage==source,就不会去loadCache,而是loadSource。(开线程加载大概流程感觉就像是默认先去缓存中找,没找到就重新加载)


private void onLoadFailed(Exception e) {
   if (isDecodingFromCache()) {
     stage = Stage.SOURCE;
     manager.submitForSource(this);
   } else {
     manager.onException(e);
   }
 }

loadSource会一路走到


private Resource<T> decodeFromSourceData(A data) throws IOException {
   final Resource<T> decoded;
   if (diskCacheStrategy.cacheSource()) {
     decoded = cacheAndDecodeSourceData(data);
   } else {
     long startTime = LogTime.getLogTime();
     decoded = loadProvider.getSourceDecoder().decode(data, width, height);
     if (Log.isLoggable(TAG, Log.VERBOSE)) {
       logWithTimeAndKey("Decoded from source", startTime);
     }
   }

这里回调的decode就是项目中自己设置的sourceDecoder

项目内的代码象征性的打码:

浅谈Glide缓存key的问题

之前id和宽高传的不一样,导致key不一样,然后Glide加载的时候通过key找不到缓存,最后就又回调到项目里面的decode那里来了。

改完后,第一次decode完后,后面用缓存就不会再进入decode了。

来源:https://www.jianshu.com/p/22b677d88b55

标签:Glide,缓存,key
0
投稿

猜你喜欢

  • Java ShutdownHook原理详解

    2023-11-10 21:30:36
  • Android 获取设备屏幕大小的几种方法总结

    2022-11-30 14:29:07
  • Java HashMap底层实现原理

    2023-10-31 19:55:34
  • Flutter web bridge 通信总结分析详解

    2022-05-23 05:15:42
  • kotlin中EditText赋值Type mismatch方式

    2023-06-19 03:52:56
  • Java实现获取指定个数的不同随机数

    2023-11-14 21:42:34
  • Springboot导出文件,前端下载文件方式

    2023-07-21 11:27:05
  • java swing实现简单计算器界面

    2021-11-09 12:47:05
  • Java 面向对象通过new揭开对象实例化

    2021-09-30 20:19:05
  • 解决IDEA鼠标点击光标变大问题

    2022-12-07 11:52:37
  • Java ArrayDeque使用方法详解

    2022-02-09 08:00:23
  • C# PDF Page操作设置页面切换按钮的方法

    2021-11-18 04:33:43
  • java 中String.equals和==的比较

    2023-03-09 08:45:16
  • 一文带你学会Spring JDBC的使用

    2023-11-29 17:05:34
  • Java实现经典游戏打砖块游戏的示例代码

    2021-06-25 13:30:16
  • 解决Android MediaRecorder录制视频过短问题

    2023-04-24 01:47:56
  • SpringCloud开发课程查询功能

    2022-04-21 14:22:46
  • C#及WPF获取本机所有字体和颜色的方法

    2022-12-23 01:23:13
  • android recyclerview模拟聊天界面

    2022-01-19 17:02:42
  • MyBatis的SQL执行结果和客户端执行结果不一致问题排查

    2022-12-30 20:37:22
  • asp之家 软件编程 m.aspxhome.com