georstat/react-native-image-cache

Question: getPath not clear to me

saif-o99 opened this issue · 7 comments

Thank you for this amazing lib, its the best for caching images.
i just want to get a clear idea of how it works and i reached to this function but its not clear to me what's the job of downloadPromise and pathResolved, if you can explain it quickly i would appreciate it.

async getPath(): Promise<string | undefined> {

  async getPath(): Promise<string | undefined> {
    const { source, maxAge, noCache } = this;
    const { exists, path, tmpPath } = await getCacheEntry(source, maxAge);

    if (exists && !noCache) {
      return path;
    }

    if (!this.downloadPromise) {
      this.pathResolved = false;
      this.downloadPromise = this.download(path, tmpPath);
    }

    if (this.downloadPromise && this.pathResolved) {
      this.pathResolved = false;
      this.downloadPromise = this.download(path, tmpPath);
    }
    return this.downloadPromise;
  }

Hi @saif-o99, these promises are in place because of this: wcandillon/react-native-expo-image-cache#126

What they actually do is to protect the app from downloading the same image again and again when requesting a single image multiple times in the same screen/view of the app.

@efstathiosntonas Thank you, the only issue which i think exist is when the images are different the pruneCache runs 2 times
which considers expensive function, am i right?

for more explanation, i added two cache image component with two different urls, both entered to download which is fine, but then both entered to pruneCache while one of them should do the job because if we have 10 images the pruneCache will run 10 times and etc..
image

Yes, pruneCache will run for every new image downloaded otherwise it won't know if you reached your cache limits thus deleting older images.

the overhead is small if the screen contains a few images. You can test performance on real device by monitoring the time it takes, check this: https://dev.to/saranshk/how-to-measure-javascript-execution-time-5h2

Try to test with cacheLimit: 0 (if 0 no pruneCache will run) and cacheLimit: 1024 * 1024 * 512 and see the differences.

Between tests make sure you clear the images from the cache or use different urls everytime.

Yes, pruneCache will run for every new image downloaded otherwise it won't know if you reached your cache limits thus deleting older images.

i was thinking that when multiple images are downloading at the same time, running pruneCache multiple times is useless because in this case it always has the same result
so if we have 10 images downloading, the pruneCache functions ( sort, reduce, while ) will run 10 times.

the overhead is small if the screen contains a few images. You can test performance on real device by monitoring the time it takes, check this: https://dev.to/saranshk/how-to-measure-javascript-execution-time-5h2

Try to test with cacheLimit: 0 (if 0 no pruneCache will run) and cacheLimit: 1024 * 1024 * 512 and see the differences.

Between tests make sure you clear the images from the cache or use different urls everytime.

sorry i just saw this comment, i'll make sure to do that later