2013-05-13 63 views
0

所以,我有一個問題緩存在java集合內的數據。當我初始化我第一次使用這個函數的這樣的應用程序,使用集合緩存數據

cacheImageAndSounds(0,6); 

現在,一旦我達到從一開始四號位我想從集合中刪除之前的3個元素並緩存在未來3即

cacheImageAndSounds(4,10); 

但正如我在高速緩存中第四,第五和第六形象已經我不會想重新給他們帶來的緩存中,因爲它們已經存在,因此我只會看下載或取第7到第10張圖像和聲音文件。

我該如何去做這件事,或者我可以如何調整我的緩存數據在地圖內的算法?

這是我用來在一個集合中創建圖像和聲音文件緩存的功能,並根據各種索引值進一步使用它來從中檢索數據。我以某種方式使用它,以便知道我可以設置兩個索引並在集合中獲取所需的數據。

public int cacheImageAndSounds(int startIndex,int lastIndex) 
    { 
     for(int i=startIndex;i<lastIndex;i++) 
     { 
     aq.ajax(data1.get(i), Bitmap.class, new AjaxCallback<Bitmap>() { 
      @Override 
      public void callback(String url, Bitmap object, AjaxStatus status) { 
       imageFilexxS.put(url, object); 
       System.out.println("size of imagefile"+imageFilexxS.size()); 
      } 
     }); 

     aq.ajax(data1.get(i).replace(".png", ".mp3"), File.class, new AjaxCallback<File>() { 
      @Override 
      public void callback(String url, File object, AjaxStatus status) { 
       imageFilexxSm.put(url, object); 

       System.out.println("size of songfile"+imageFilexxSm.size()); 

       if(imageFilexxSm.size()>=6) 
       { 
         update(); //call to the UI 
       } 
      } 
     }); 
     } 
     return 1; 
    } 

清除緩存並構建新的緩存。

public void clearCacheLogic() 
    { 
     imageFilexxS.clear(); 
     imageFilexxSm.clear(); 
    } 

回答

2

您似乎不緩存指數和檢查它,使得AJAX調用之前。有一個新的Set<Integer>調用processed。和方法一樣,

public int cacheImageAndSounds(int startIndex,int lastIndex) 
    { 
     for(final int i=startIndex;i<lastIndex;i++) 
     { 
      //check if the index is already processed, if not then make the call 
      if(!processed.contains(i)) { 
       aq.ajax(data1.get(i), Bitmap.class, new AjaxCallback<Bitmap>() { 
        @Override 
        public void callback(String url, Bitmap object, AjaxStatus status) { 
         imageFilexxS.put(url, object); 
         System.out.println("size of imagefile"+imageFilexxS.size()); 
         processed.add(i); //once the result comes, mark the index as processed 
        } 
       }); 

       aq.ajax(data1.get(i).replace(".png", ".mp3"), File.class, new AjaxCallback<File>() { 
        @Override 
        public void callback(String url, File object, AjaxStatus status) { 
         imageFilexxSm.put(url, object); 
         processed.add(i); //once the result comes, mark the index as processed 
         System.out.println("size of songfile"+imageFilexxSm.size()); 

         if(imageFilexxSm.size()>=6) 
         { 
          update(); //call to the UI 
         } 
        } 
       }); 
      } 
     } 
     return 1; 
    } 

這樣,當你調用cacheImageAndSounds(4,10);,爲第4,第5和第6的索引,沒有Ajax調用將進行,因爲這些指標都已經出現在processed設置

+0

但是,一旦我到達特定索引,我必須從處理過的'Set'中移除元素,並且我想緩存下一個。例如,正如我在我的問題中已經提到的那樣 - >我在第4張圖片上,我想從「Map」中刪除以前的圖片,以便地圖保留其中的內存。因此,我需要再次清除「Set」,我的意思是前3個,然後讓我說回到第一個,然後我會明確清除最後三個,然後Set會得到新的位置。這應該是明確的.. – Prateek 2013-05-13 07:57:37