【问题标题】:Why there is a mWaitingRequests in RequestQueue in Volley为什么 Volley 的 RequestQueue 中有一个 mWaitingRequests
【发布时间】:2016-04-01 03:36:57
【问题描述】:

我知道 mWaitingRequest 会保留具有相同 cacheKey 的请求,当一个 Request 完成时,具有相同 cacheKey 的请求会被添加到 mCacheQueue

但是我觉得没必要,为什么不直接把具有相同cacheKey的请求加入mCacheQueue呢?

我只是搜索谷歌,但没有得到答案。

【问题讨论】:

    标签: android asynchronous android-volley


    【解决方案1】:

    因为那样他们就没有缓存了,所有都将进入网络队列,你不想要那个

    【讨论】:

      【解决方案2】:

      具有相同cacheKey的请求将被添加到mCacheQueue中

      不,只有在必须缓存的情况下才添加请求,再看source code

      <T> void finish(Request<T> request) {
              ...
      
              if (request.shouldCache()) {
                  synchronized (mWaitingRequests) {
                      String cacheKey = request.getCacheKey();
                      Queue<Request<?>> waitingRequests = mWaitingRequests.remove(cacheKey);
                      if (waitingRequests != null) {
                          if (VolleyLog.DEBUG) {
                              VolleyLog.v("Releasing %d waiting requests for cacheKey=%s.",
                                      waitingRequests.size(), cacheKey);
                          }
                          // Process all queued up requests. They won't be considered as in flight, but
                          // that's not a problem as the cache has been primed by 'request'.
                          mCacheQueue.addAll(waitingRequests);
                      }
                  }
              }
          }
      

      为什么不直接将具有相同cacheKey的请求添加到mCacheQueue 直接?

      首先你应该注意服务器决定缓存策略,例如服务器可能不允许你缓存数据并将http header的缓存字段设置为:

      cache-control: private, max-age=0, no-cache
      

      这意味着对同一个URL的每个新请求都可以有不同的响应,可以有新的响应,这意味着服务器响应可以随时更改并且不能被缓存。现在如果用户想要缓存响应并且已经发出了多个请求,每个请求可能会有一个新的响应,所以为了简单起见,如果用户想要缓存数据,每个请求都必须分派到NetworkDispatcher

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-09-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多