2013-07-15 45 views
6

我想顯示一個列表視圖與很多(遠程)圖像。我正在嘗試使用抽籤。排球和位圖緩存

排球有些作品,但不夠好。在ImageLoader.get凌空具有下面的代碼段:

final String cacheKey = getCacheKey(requestUrl, maxWidth, maxHeight); 

    // Try to look up the request in the cache of remote images. 
    Bitmap cachedBitmap = mCache.getBitmap(cacheKey); 
    if (cachedBitmap != null) { 
     // Return the cached bitmap. 
     ImageContainer container = new ImageContainer(cachedBitmap, requestUrl, null, null); 
     imageListener.onResponse(container, true); 
     return container; 
    } 

然而,getCacheKey產生一個關鍵是這樣的:

/** 
* Creates a cache key for use with the L1 cache. 
* @param url The URL of the request. 
* @param maxWidth The max-width of the output. 
* @param maxHeight The max-height of the output. 
*/ 
private static String getCacheKey(String url, int maxWidth, int maxHeight) { 
    return new StringBuilder(url.length() + 12).append("#W").append(maxWidth) 
      .append("#H").append(maxHeight).append(url).toString(); 
} 

即它附加一些像的寬度和高度與鍵「元數據」。

此密鑰從來沒有產生一擊,如果圖像不在L1緩存它在線提取。當圖像在線獲取時,它會保存在磁盤緩存中,但Volley會以URL(僅URL)作爲關鍵字來保存它。

這是預期的行爲?我錯過了什麼嗎?

+0

它可能取決於您的迴應標題 – njzk2

+1

你知道爲什麼凌空不會產生熱門嗎? – iamrelos

+0

您可以使用[droidQuery](http://bit.ly/droidquery)來完成異步請求緩存(Ajax),並且控制緩存更容易(無論是否,以及保留緩存對象多長時間)。 – Phil

回答

-1

這是您希望它工作的確切方式。

  1. 點擊網址,並獲取圖片時,它不可用。
  2. 從緩存中加載圖像(如果可用)。
+0

這沒有意義。首先,您要在點擊url之前從緩存中加載圖像。其次,這並不回答我的問題,關於如何排除工作 – Markus

+0

我理解你的問題,就像你在上面的評論中提到的方式一樣。 –

+0

我不明白你在說什麼。我問「爲什麼內部高速緩存永遠不會產生命中」 – Markus

1

你可以發佈你的類實現ImageCache。

我剛纔一直在尋找這個自己,並在我的代碼中實現我沒有將位圖添加到內存緩存中,因此它每次都會從磁盤重新加載它。

這是我的意思一個簡單的例子,我要去哪裏錯了

@Override 
    public Bitmap getBitmap(String cachKey) { 

     Bitmap b = null; 

      //check the memory first 
      b = memoryCache.get(cacheKey); 
      if(b == null){ 
       //memory cache was null, check file cache   
       b = diskLruImageCache.getBitmap(cacheKey); 

       // this is where it needs to be added to your memory cache 
       if(b != null){ 
        memoryCache.put(url, b); 
       } 
      } 



     return b; 
    } 
9

你沒有得到任何匹配的原因是因爲磁盤緩存在凌空的默認行爲是依賴於HTTP您請求的元素的標題(在您的情況下,圖像)。

排球的工作方式是:

  1. ImageLoader檢查L1高速緩存(由您在其構造提供給ImageLoader內存緩存)。如果可用返回圖像。
  2. 請求處理RequestQueue。它檢查圖像的L2(磁盤緩存)。
  3. 如果在磁盤緩存中找到,請檢查映像到期時間。如果沒有過期,則返回。
  4. 下載圖像並返回。
  5. 將圖像保存在緩存中。

如果你想在默認的設置工作,圖像必須有一個Cache-Control頭像max-age=???其中問號表示從其所下載的時間足夠秒。

如果您想更改默認行爲,我不確定,但我認爲您必須編輯一些代碼。

查看Volley資源中的CacheDispatcher類。

1

我今天在自己的應用中追蹤了這個問題。我在構造函數中設置了以KB爲單位的最大緩存大小,但是在sizeOf()中報告了以字節爲單位的大小,因此沒有任何緩存。

This answer給我挺身而出。

0

如果在響應頭中沒有設置緩存控制,則排氣不會緩存任何東西。

檢查HttpHeaderParser Volley中的類實現。

緩存可以基於最大年齡或電子標籤。檢查您的響應標題並確定設置的任何內容。它看起來像這樣。

的Cache-Control→公衆,最大年齡= 300

Cache Header info

1

也許你正在使用NetworkImageView加載圖像。您可以使用ImageView和ImageLoader來做同樣的事情。對於任何圖像大小,使用ImageLoader時,密鑰中的元數據就像「#W0#H0」。

ImageLoader imageLoader = getImageLoader(); 
imageLoader.get(url, ImageLoader.getImageListener(imageView, defaultDrawable, errorDrawable));