使用集合缓存数据

时间:2013-05-13 07:37:17

标签: java algorithm

所以,我在java集合中缓存数据时遇到问题。当我第一次初始化应用程序时,我使用下面的函数,

cacheImageAndSounds(0,6);

现在,一旦我从头开始到达第4个位置,我想从集合中删除前3个元素并缓存下3个

cacheImageAndSounds(4,10);

但是因为我已经在缓存中存在第4,第5和第6个图像,所以我不想将它们重新放入缓存中,因为它们已经存在,因此我只想下载或获取第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();
    }

1 个答案:

答案 0 :(得分: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中已存在这些索引设置