Fresco 緩存策略管理源碼分析(二)

前面介紹了一級(jí)緩存旗唁,現(xiàn)在看二級(jí)緩存仑嗅,需要request sumbit請(qǐng)求帶入宴倍。
你可能不熟悉界面代碼,需要看Fresco SimpleDraweeView界面源碼分析

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
       if(null == mDatas || mDatas.size() == position)
            return;

       EarnItemHolder itemHolder = (EarnItemHolder) holder;
       TapJoyBean tapJoyBean = mDatas.get(position);
        Uri uri = Uri.parse(tapJoyBean.IconURL);
        itemHolder.imageView.setImageURI(uri);
        itemHolder.textView.setText(tapJoyBean.Name);
}

簡(jiǎn)單一個(gè)列子帶入仓技,SimpleDraweeView.setImageURI(uri)開始

  /**
   * Displays an image given by the uri.
   *
   * @param uri uri of the image
   * @param callerContext caller context
   */
  public void setImageURI(Uri uri, @Nullable Object callerContext) {
    DraweeController controller = mSimpleDraweeControllerBuilder
        .setCallerContext(callerContext)
        .setUri(uri)
        .setOldController(getController())
        .build();
    setController(controller);
  }

設(shè)置完DraweeHolder和DraweeController綁定后鸵贬,DraweeHolder開始介紹界面事件,當(dāng)view:onAttach()開始submit

  /**
   * Callback used to notify about top-level-drawable's visibility changes.
   */
  @Override
  public void onVisibilityChange(boolean isVisible) {
    if (mIsVisible == isVisible) {
      return;
    }
    mEventTracker.recordEvent(isVisible ? Event.ON_DRAWABLE_SHOW : Event.ON_DRAWABLE_HIDE);
    mIsVisible = isVisible;
    attachOrDetachController();
  }

@Override
  public void onAttach() {
    if (FLog.isLoggable(FLog.VERBOSE)) {
      FLog.v(
          TAG,
          "controller %x %s: onAttach: %s",
          System.identityHashCode(this),
          mId,
          mIsRequestSubmitted ? "request already submitted" : "request needs submit");
    }
    mEventTracker.recordEvent(Event.ON_ATTACH_CONTROLLER);
    Preconditions.checkNotNull(mSettableDraweeHierarchy);
    mDeferredReleaser.cancelDeferredRelease(this);
    mIsAttached = true;
    if (!mIsRequestSubmitted) {
      submitRequest();
    }
  }

protected void submitRequest() {
    final T closeableImage = getCachedImage(); // 1
    if (closeableImage != null) {
      mDataSource = null;
      mIsRequestSubmitted = true;
      mHasFetchFailed = false;
      mEventTracker.recordEvent(Event.ON_SUBMIT_CACHE_HIT);
      getControllerListener().onSubmit(mId, mCallerContext);
      onNewResultInternal(mId, mDataSource, closeableImage, 1.0f, true, true);
      return;
    }
    mEventTracker.recordEvent(Event.ON_DATASOURCE_SUBMIT);
    getControllerListener().onSubmit(mId, mCallerContext);
    mSettableDraweeHierarchy.setProgress(0, true);
    mIsRequestSubmitted = true;
    mHasFetchFailed = false;
    mDataSource = getDataSource(); // 2
    if (FLog.isLoggable(FLog.VERBOSE)) {
      FLog.v(
          TAG,
          "controller %x %s: submitRequest: dataSource: %x",
          System.identityHashCode(this),
          mId,
          System.identityHashCode(mDataSource));
    }
    final String id = mId;
    final boolean wasImmediate = mDataSource.hasResult();
    final DataSubscriber<T> dataSubscriber =
        new BaseDataSubscriber<T>() {
          @Override
          public void onNewResultImpl(DataSource<T> dataSource) {
            // isFinished must be obtained before image, otherwise we might set intermediate result
            // as final image.
            boolean isFinished = dataSource.isFinished();
            float progress = dataSource.getProgress();
            T image = dataSource.getResult();
            if (image != null) {
              onNewResultInternal(id, dataSource, image, progress, isFinished, wasImmediate);
            } else if (isFinished) {
              onFailureInternal(id, dataSource, new NullPointerException(), /* isFinished */ true);
            }
          }
          @Override
          public void onFailureImpl(DataSource<T> dataSource) {
            onFailureInternal(id, dataSource, dataSource.getFailureCause(), /* isFinished */ true);
          }
          @Override
          public void onProgressUpdate(DataSource<T> dataSource) {
            boolean isFinished = dataSource.isFinished();
            float progress = dataSource.getProgress();
            onProgressUpdateInternal(id, dataSource, progress, isFinished);
          }
        };
    mDataSource.subscribe(dataSubscriber, mUiThreadImmediateExecutor); // 3
  }

又介紹一個(gè)sumbit方法脖捻,一開始調(diào)用getCachedImage()來自子類實(shí)現(xiàn)類PipelineDraweeController

  @Override
  protected CloseableReference<CloseableImage> getCachedImage() { // 來自1
    if (!getExperiment().mIsFastCheckEnabled) {
      return null;
    }
    if (mMemoryCache == null || mCacheKey == null) {
      return null;
    }
    // We get the CacheKey
    CloseableReference<CloseableImage> closeableImage = mMemoryCache.get(mCacheKey);
    if (closeableImage != null && !closeableImage.get().getQualityInfo().isOfFullQuality()) {
      closeableImage.close();
      return null;
    }
    return closeableImage;
  }

  @Override
  protected DataSource<CloseableReference<CloseableImage>> getDataSourceForRequest( // 來自2
      ImageRequest imageRequest,
      Object callerContext,
      boolean bitmapCacheOnly) {
    if (bitmapCacheOnly) {
      return mImagePipeline.fetchImageFromBitmapCache(imageRequest, callerContext);
    } else {
      return mImagePipeline.fetchDecodedImage(imageRequest, callerContext);
    }
  }

1:因?yàn)槲医榻B二級(jí)緩存阔逼,getExperiment()這貨拿到是個(gè)新的,如果在同一個(gè)界面地沮,沒有直接onDetch()這個(gè)對(duì)象返回值是檢查過的嗜浮,會(huì)走下面Cahe,而這個(gè)Cahce是InstrumentedMemoryCache和前面介紹的一樣摩疑,這貨初始化在Frescon.init()危融,包含了一個(gè)CoutingMemoryCache。
2:這貨從子類拿到一個(gè)datasource(model)雷袋,從supplied拿到一個(gè)流程吉殃,ImagePiple就開始干活和維護(hù)
3:這貨訂閱在ui線程,注冊(cè)到model層中楷怒,是個(gè)pair<key,value>等model層數(shù)據(jù)更新蛋勺,就刷新DraweeHierarchy
繼續(xù)看2,因?yàn)樾陆╲iew率寡,是無法確定這個(gè)key被一級(jí)緩存了迫卢,所以走mImagePipeline.fetchDecodedImage(imageRequest, callerContext);

 /**
   * Submits a request for execution and returns a DataSource representing the pending decoded
   * image(s).
   * <p>The returned DataSource must be closed once the client has finished with it.
   * @param imageRequest the request to submit
   * @return a DataSource representing the pending decoded image(s)
   */
  public DataSource<CloseableReference<CloseableImage>> fetchDecodedImage(
      ImageRequest imageRequest,
      Object callerContext) {
    try {
      Producer<CloseableReference<CloseableImage>> producerSequence =
          mProducerSequenceFactory.getDecodedImageProducerSequence(imageRequest); // 4
      return submitFetchRequest( // 5
          producerSequence,
          imageRequest,
          ImageRequest.RequestLevel.FULL_FETCH,
          callerContext);
    } catch (Exception exception) {
      return DataSources.immediateFailedDataSource(exception);
    }
  }

/**
   * Returns a sequence that can be used for a request for a decoded image.
   *
   * @param imageRequest the request that will be submitted
   * @return the sequence that should be used to process the request
   */
  public Producer<CloseableReference<CloseableImage>> getDecodedImageProducerSequence(
      ImageRequest imageRequest) { // 來自4
    Producer<CloseableReference<CloseableImage>> pipelineSequence =
        getBasicDecodedImageSequence(imageRequest);
    if (imageRequest.getPostprocessor() != null) { // 這貨后處理器,這里為null冶共,因?yàn)闆]有特殊定制image乾蛤,有興趣翻我第一章
      return getPostprocessorSequence(pipelineSequence);
    } else {
      return pipelineSequence;
    }
  }


protected AbstractProducerToDataSourceAdapter( // 來自5
      Producer<T> producer,
      SettableProducerContext settableProducerContext,
      RequestListener requestListener) {
    mSettableProducerContext = settableProducerContext;
    mRequestListener = requestListener;
    mRequestListener.onRequestStart(
        settableProducerContext.getImageRequest(),
        mSettableProducerContext.getCallerContext(),
        mSettableProducerContext.getId(),
        mSettableProducerContext.isPrefetch());
    producer.produceResults(createConsumer(), settableProducerContext); // 6
  }

  private Consumer<T> createConsumer() { // 來自6
    return new BaseConsumer<T>() {
      @Override
      protected void onNewResultImpl(@Nullable T newResult, boolean isLast) {// 來自9的回調(diào)
        AbstractProducerToDataSourceAdapter.this.onNewResultImpl(newResult, isLast); // 生成11
      }

      @Override
      protected void onFailureImpl(Throwable throwable) {
        AbstractProducerToDataSourceAdapter.this.onFailureImpl(throwable);
      }

      @Override
      protected void onCancellationImpl() {
        AbstractProducerToDataSourceAdapter.this.onCancellationImpl();
      }

      @Override
      protected void onProgressUpdateImpl(float progress) {
        AbstractProducerToDataSourceAdapter.this.setProgress(progress);
      }
    };
  }

又回到第一章我講的request流程了每界。
4:會(huì)創(chuàng)建很多Producer和判斷磁盤Cache,還有NewWorkFetch(下載底層類)
5:其實(shí)回掉接口家卖,和JobService安排任務(wù)
然后這個(gè)producer就到BitmapMemoryCacheProducer

 @Override
  public void produceResults( // 來自6
      final Consumer<CloseableReference<CloseableImage>> consumer,
      final ProducerContext producerContext) {

    final ProducerListener listener = producerContext.getListener();
    final String requestId = producerContext.getId();
    listener.onProducerStart(requestId, getProducerName());
    final ImageRequest imageRequest = producerContext.getImageRequest();
    final Object callerContext = producerContext.getCallerContext();
    final CacheKey cacheKey = mCacheKeyFactory.getBitmapCacheKey(imageRequest, callerContext); // 7

    CloseableReference<CloseableImage> cachedReference = mMemoryCache.get(cacheKey); // 8

    if (cachedReference != null) {
      boolean isFinal = cachedReference.get().getQualityInfo().isOfFullQuality(); // 9
      if (isFinal) {
        listener.onProducerFinishWithSuccess(
            requestId,
            getProducerName(),
            listener.requiresExtraMap(requestId) ? ImmutableMap.of(VALUE_FOUND, "true") : null);
        consumer.onProgressUpdate(1f);
      }
      consumer.onNewResult(cachedReference, isFinal); // 10
      cachedReference.close();
      if (isFinal) {
        return;
      }
    }

    if (producerContext.getLowestPermittedRequestLevel().getValue() >=
        ImageRequest.RequestLevel.BITMAP_MEMORY_CACHE.getValue()) {
      listener.onProducerFinishWithSuccess(
          requestId,
          getProducerName(),
          listener.requiresExtraMap(requestId) ? ImmutableMap.of(VALUE_FOUND, "false") : null);
      consumer.onNewResult(null, true);
      return;
    }

    Consumer<CloseableReference<CloseableImage>> wrappedConsumer = wrapConsumer(consumer, cacheKey);
    listener.onProducerFinishWithSuccess(
        requestId,
        getProducerName(),
        listener.requiresExtraMap(requestId) ? ImmutableMap.of(VALUE_FOUND, "false") : null);
    mInputProducer.produceResults(wrappedConsumer, producerContext);
  }


  @Override
  public CacheKey getBitmapCacheKey(ImageRequest request, Object callerContext) { // 來自7
    return new BitmapMemoryCacheKey(
        getCacheKeySourceUri(request.getSourceUri()).toString(),
        request.getResizeOptions(),
        request.getAutoRotateEnabled(),
        request.getImageDecodeOptions(),
        null,
        null,
        callerContext);
  }


 @Override
  public CloseableReference<V> get(K key) { // 來自8
    CloseableReference<V> result = mDelegate.get(key);
    if (result == null) {
      mTracker.onCacheMiss();
    } else {
      mTracker.onCacheHit();
    }
    return result;
  }

7:會(huì)利用DefautCacheFactory根據(jù)ImageRequest創(chuàng)建一個(gè)key
8:就是前面的InstrumentedMemoryCache眨层,這里因?yàn)榫彺孢^,所以直接hit了
9:如果是下載完整的image上荡,這個(gè)值為true
10:直接回掉回去了趴樱,后面代碼是請(qǐng)求request安排任務(wù)了,然后做完再創(chuàng)建bitmapCache

public abstract class AbstractDataSource<T> implements DataSource<T> {
  /**
   * Subclasses should invoke this method to set the result to {@code value}.
   *
   * <p> This method will return {@code true} if the value was successfully set, or
   * {@code false} if the data source has already been set, failed or closed.
   *
   * <p> If the value was successfully set and {@code isLast} is {@code true}, state of the
   * data source will be set to {@link AbstractDataSource.DataSourceStatus#SUCCESS}.
   *
   * <p> {@link #closeResult} will be called for the previous result if the new value was
   * successfully set, OR for the new result otherwise.
   *
   * <p> This will also notify the subscribers if the value was successfully set.
   *
   * <p> Do NOT call this method from a synchronized block as it invokes external code of the
   * subscribers.
   *
   * @param value the value that was the result of the task.
   * @param isLast whether or not the value is last.
   * @return true if the value was successfully set.
   */
  protected boolean setResult(@Nullable T value, boolean isLast) { // 來自11
    boolean result = setResultInternal(value, isLast); // 12
    if (result) {
      notifyDataSubscribers();
    }
    return result;
  }


  private void notifyDataSubscribers() { // 來自12酪捡, 13
    final boolean isFailure = hasFailed();
    final boolean isCancellation = wasCancelled();
    for (Pair<DataSubscriber<T>, Executor> pair : mSubscribers) {
      notifyDataSubscriber(pair.first, pair.second, isFailure, isCancellation);
    }
  }
}

 private void notifyDataSubscriber( // 15
      final DataSubscriber<T> dataSubscriber,
      final Executor executor,
      final boolean isFailure,
      final boolean isCancellation) {
    executor.execute(
        new Runnable() {
          @Override
          public void run() {
            if (isFailure) {
              dataSubscriber.onFailure(AbstractDataSource.this);
            } else if (isCancellation) {
              dataSubscriber.onCancellation(AbstractDataSource.this);
            } else {
              dataSubscriber.onNewResult(AbstractDataSource.this);
            }
          }
        });

 @Override
  public void subscribe(final DataSubscriber<T> dataSubscriber, final Executor executor) { // 14
    Preconditions.checkNotNull(dataSubscriber);
    Preconditions.checkNotNull(executor);
    boolean shouldNotify;

    synchronized(this) {
      if (mIsClosed) {
        return;
      }

      if (mDataSourceStatus == DataSourceStatus.IN_PROGRESS) {
        mSubscribers.add(Pair.create(dataSubscriber, executor));
      }

      shouldNotify = hasResult() || isFinished() || wasCancelled();
    }

    if (shouldNotify) {
      notifyDataSubscriber(dataSubscriber, executor, hasFailed(), wasCancelled());
    }
  }
  }

13:其實(shí)只有下載完成才回調(diào)那個(gè)函數(shù)叁征,那個(gè)pai是根據(jù)狀態(tài)來add的,一開始為DataSourceStatus.IN_PROGRESS逛薇,然后重新創(chuàng)建生產(chǎn)者和AbstractDataSource的時(shí)候捺疼,Cache命中了,就跳過了add過程永罚,下載完成才有這個(gè)
14:是submit創(chuàng)建啤呼,都在主線程的,先要判斷一堆Cache和生產(chǎn)者
15:是最終的回掉view了
三級(jí)緩存

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末呢袱,一起剝皮案震驚了整個(gè)濱河市官扣,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌羞福,老刑警劉巖惕蹄,帶你破解...
    沈念sama閱讀 217,907評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異坯临,居然都是意外死亡焊唬,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,987評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門看靠,熙熙樓的掌柜王于貴愁眉苦臉地迎上來赶促,“玉大人,你說我怎么就攤上這事挟炬∨副酰” “怎么了?”我有些...
    開封第一講書人閱讀 164,298評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵谤祖,是天一觀的道長(zhǎng)婿滓。 經(jīng)常有香客問我,道長(zhǎng)粥喜,這世上最難降的妖魔是什么凸主? 我笑而不...
    開封第一講書人閱讀 58,586評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮额湘,結(jié)果婚禮上卿吐,老公的妹妹穿的比我還像新娘旁舰。我一直安慰自己,他們只是感情好嗡官,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,633評(píng)論 6 392
  • 文/花漫 我一把揭開白布箭窜。 她就那樣靜靜地躺著,像睡著了一般衍腥。 火紅的嫁衣襯著肌膚如雪磺樱。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,488評(píng)論 1 302
  • 那天婆咸,我揣著相機(jī)與錄音竹捉,去河邊找鬼。 笑死擅耽,一個(gè)胖子當(dāng)著我的面吹牛活孩,可吹牛的內(nèi)容都是我干的物遇。 我是一名探鬼主播乖仇,決...
    沈念sama閱讀 40,275評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼询兴!你這毒婦竟也來了乃沙?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,176評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤诗舰,失蹤者是張志新(化名)和其女友劉穎警儒,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體眶根,經(jīng)...
    沈念sama閱讀 45,619評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡蜀铲,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,819評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了属百。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片记劝。...
    茶點(diǎn)故事閱讀 39,932評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖族扰,靈堂內(nèi)的尸體忽然破棺而出厌丑,到底是詐尸還是另有隱情,我是刑警寧澤渔呵,帶...
    沈念sama閱讀 35,655評(píng)論 5 346
  • 正文 年R本政府宣布怒竿,位于F島的核電站,受9級(jí)特大地震影響扩氢,放射性物質(zhì)發(fā)生泄漏耕驰。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,265評(píng)論 3 329
  • 文/蒙蒙 一录豺、第九天 我趴在偏房一處隱蔽的房頂上張望朦肘。 院中可真熱鬧托嚣,春花似錦、人聲如沸厚骗。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,871評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽领舰。三九已至夫嗓,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間冲秽,已是汗流浹背舍咖。 一陣腳步聲響...
    開封第一講書人閱讀 32,994評(píng)論 1 269
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留锉桑,地道東北人排霉。 一個(gè)月前我還...
    沈念sama閱讀 48,095評(píng)論 3 370
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像民轴,于是被迫代替她去往敵國(guó)和親攻柠。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,884評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,128評(píng)論 25 707
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理后裸,服務(wù)發(fā)現(xiàn)瑰钮,斷路器,智...
    卡卡羅2017閱讀 134,656評(píng)論 18 139
  • Volley源碼分析之流程和緩存 前言 Android一開始提供了HttpURLConnection和HttpCl...
    大寫ls閱讀 619評(píng)論 0 6
  • Xutils3.0技術(shù)分享1.這個(gè)技術(shù)分享的目的1.首先要讓大家了解Xutil3.0是什么Xtuils3.0的前身...
    wodezhuanshu閱讀 3,115評(píng)論 5 9
  • 經(jīng)過一宿微驶,在去福隆的臺(tái)鐵上終于有精力來寫一寫來臺(tái)北的第一夜浪谴,是我和室友可以記到下輩子的一夜,精(生)彩(無)絕...
    狀況少女閱讀 826評(píng)論 5 2