欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

浅谈Glide缓存key的问题

时间:2020-10-18 14:09:50|栏目:Android代码|点击:

最近项目里面有个地方是在前面用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

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


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

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

上一篇:Android实现音乐播放器锁屏页

栏    目:Android代码

下一篇:Android studio 3.0 查看手机文件系统的方法(超简单)

本文标题:浅谈Glide缓存key的问题

本文地址:http://www.codeinn.net/misctech/13356.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有