Android源码分析Android篇

Glide缓存机制浅析

2021-04-20  本文已影响0人  w达不溜w
Glide.with(this).load(imageUrl).into(imageView);

into调用链:

  into(imageView)
—>RequestBuilder#into(...)
—>requestManager.track(target, request)
—>RequestTracker#runRequest()
—>request.begin()
—>SingleRequest#begin()
—>onSizeReady()
—>engine.load

来到Engine的load

public synchronized <R> LoadStatus load(
      GlideContext glideContext,
      Object model,
      Key signature,
      int width,
      int height,
      Class<?> resourceClass,
      Class<R> transcodeClass,
      Priority priority,
      DiskCacheStrategy diskCacheStrategy,
      Map<Class<?>, Transformation<?>> transformations,
      boolean isTransformationRequired,
      boolean isScaleOnlyOrNoTransform,
      Options options,
      boolean isMemoryCacheable,
      boolean useUnlimitedSourceExecutorPool,
      boolean useAnimationPool,
      boolean onlyRetrieveFromCache,
      ResourceCallback cb,
      Executor callbackExecutor) {
    
    long startTime = VERBOSE_IS_LOGGABLE ? LogTime.getLogTime() : 0;
    //①构建缓存key
    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;
    }
    //③从内存缓存中查找
    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<?> current = jobs.get(key, onlyRetrieveFromCache);
    if (current != null) {
      //如果有回调出去
      current.addCallback(cb, callbackExecutor);
      if (VERBOSE_IS_LOGGABLE) {
        logWithTimeAndKey("Added to existing load", startTime, key);
      }
      return new LoadStatus(cb, current);
    }

    //上面的3种方式都没有获取到,就构建一个新的请求任务。
    //创建执行任务
    EngineJob<R> engineJob =
        engineJobFactory.build(
            key,
            isMemoryCacheable,
            useUnlimitedSourceExecutorPool,
            useAnimationPool,
            onlyRetrieveFromCache);
    //创建解码工作,用于处理图片的
    DecodeJob<R> decodeJob =
        decodeJobFactory.build(
            glideContext,
            model,
            key,
            signature,
            width,
            height,
            resourceClass,
            transcodeClass,
            priority,
            diskCacheStrategy,
            transformations,
            isTransformationRequired,
            isScaleOnlyOrNoTransform,
            onlyRetrieveFromCache,
            options,
            engineJob);
        //缓存请求任务
    jobs.put(key, engineJob);
        //添加执行任务的回调
    engineJob.addCallback(cb, callbackExecutor);
    //开始执行
    engineJob.start(decodeJob);

    if (VERBOSE_IS_LOGGABLE) {
      logWithTimeAndKey("Started new load", startTime, key);
    }
    return new LoadStatus(cb, engineJob);
  }

Glide的三级缓存:

活动缓存:使用弱引用缓存图片,表正在使用的图片,图片回收后,保存到内存缓存中。(活动缓存存在的意义:防止内存缓存因LRU策略,正在使用的图片被回收的问题

内存缓存:如果活动缓存找不到图片,就从内存缓存中查找,找到后”移动“到活动缓存。

磁盘缓存:上面两级缓存都没找到,就从磁盘缓存中查找,找到后”复制“到活动缓存。

glide缓存机制.png
上一篇 下一篇

猜你喜欢

热点阅读