Performance issue with Volley's DiskBasedCache

前端 未结 3 1078
耶瑟儿~
耶瑟儿~ 2020-12-23 16:55

In my Photo Collage app for Android I\'m using Volley for loading images. I\'m using the DiskBasedCache (included with volley) with 50 mb storage to prevent re-downloading

3条回答
  •  执笔经年
    2020-12-23 17:25

    Yes, the way DiskBasedCache works it needs to open all the files in initialize(). Which is simply.... not a good idea :-(

    You need to make a different implementation that doesent open all the files at startup.

    Take a copy of DiskBasedCache and change initialize() to

      @Override
      public synchronized void initialize() {
        if (!mRootDirectory.exists()) {
          if (!mRootDirectory.mkdirs()) {
            VolleyLog.e("Unable to create cache dir %s", mRootDirectory.getAbsolutePath());
          }
        }
      }
    

    And change get() so it makes an additional check for if the file exists on the file system, like

      @Override
      public synchronized Entry get(String key) {
        CacheHeader entry = mEntries.get(key);
        File file = getFileForKey(key);
        if (entry == null && !file.exists()) { // EXTRA CHECK
          // if the entry does not exist, return.
          VolleyLog.d("DrVolleyDiskBasedCache miss for " + key);
          return null;
        }
        ...
    

    I use this approach in https://play.google.com/store/apps/details?id=dk.dr.radio and it works fine - its robustness have been tested by ~300000 users :-)

    You can download a full version of the file from https://code.google.com/p/dr-radio-android/source/browse/trunk/DRRadiov3/src/dk/dr/radio/net/volley/DrDiskBasedCache.java (you'll have to delete some DR Radio specific stuff)

提交回复
热议问题