美文网首页
Glide新版本缓存机制(V4.9)

Glide新版本缓存机制(V4.9)

作者: 帅哥不帅123 | 来源:发表于2019-10-09 17:46 被阅读0次

    RequestBuilder.into()->RequestManager.trank()->RequestTracker.runRequest()->SingleRequest.begin()

    ->onSizeReady()->最终到Engine.load()方法:

    主要代码如下:

    EngineKey key =keyFactory.buildKey(model, signature, width, height, transformations,

        resourceClass, transcodeClass, options);

    //从弱引用中取

    EngineResource active = loadFromActiveResources(key, isMemoryCacheable);

    if (active !=null) {

    cb.onResourceReady(active, DataSource.MEMORY_CACHE);

      if (VERBOSE_IS_LOGGABLE) {

    logWithTimeAndKey("Loaded resource from active resources", startTime, key);

      }

    return null;

    }

    //从LRUcache中取,取到了移动到弱引用中去

    EngineResource cached = loadFromCache(key, isMemoryCacheable);

    if (cached !=null) {

    cb.onResourceReady(cached, DataSource.MEMORY_CACHE);

      if (VERBOSE_IS_LOGGABLE) {

    logWithTimeAndKey("Loaded resource from cache", startTime, key);

      }

    return null;

    }

    //下载逻辑

    接上一步,如果在缓存里没找到,则会进行下载流程:

    engineJob.start(decodeJob)----> DecodeJob decodeJob .run()->runWrapped()->startNext()>SourceGenerator.startNext()->

    loadData.fetcher.loadData(helper.getPriority(), this);  //这步根据不同Fetcher来选择处理方式,

    这里看HttpUrlFetcher,里面最终执行loadDataWithRedirects()方法看到了我们熟悉的urlConnection,

    下载完数据后,回调callback.onDataReady(result);方法返回数据

    SourceGenerator.onDataReady()方法会判断diskCacheStrategy缓存策略,如果可以混存则记录下来(这块不是太理解),否则执行DecodeJob.onDataFetcherReady(),该方法会判断是否当前线程,如果不是则改变runReason = RunReason.DECODE_DATA状态,执行extor最终会执行本地run方法,再次调用run()->runWrapped()方法,这次是DECODE_DATA流程,则执行DecodeJob.decodeFromRetrievedData()方法->notifyEncodeAndRelease()->notifyComplete()->回到EngineJob.notifyCallbacksOfResult()->最终调用回

    Engine.onEngineJobComplete();

    @SuppressWarnings("unchecked")

    @Override

    public synchronized void onEngineJobComplete(

    EngineJob engineJob, Key key, EngineResource resource) {

    // A null resource indicates that the load failed, usually due to an exception.

      if (resource !=null) {

    resource.setResourceListener(key, this);

        if (resource.isCacheable()) {//缓存处理

    activeResources.activate(key, resource);

        }

    }

    jobs.removeIfCurrent(key, engineJob);

    }

    其他:

    配置下载fetcher, 调用的时候Glide.with(context)会调用build流程初始化下载参数

    其他优秀博客 :

    https://www.jianshu.com/p/9d8aeaa5a329

    相关文章

      网友评论

          本文标题:Glide新版本缓存机制(V4.9)

          本文链接:https://www.haomeiwen.com/subject/dcggpctx.html