Best Practice when Caching files in Android

前端 未结 3 1059
执念已碎
执念已碎 2021-01-30 02:42

I currently have my app caching image files in the cache sub-directory for the application. The images are used in a ListView and stored in a Has

相关标签:
3条回答
  • 2021-01-30 03:09

    Should I include an option to choose the location of the cache?

    IMO: No, let make it more simplest as possible (Except you can include advance setting for expert user)

    Should I attempt to manage the size of my cache (be it in the /cache or /sdcard) or just forget about it?

    IMO: This is optional, it is double sword: your more work on background will help user more convenience but also more bug prone

    Use 3rd libs: IMO using 3rd library as Picasso is better, it handle cache automatically by order: Memory cache -> Disk cache -> Network

    0 讨论(0)
  • 2021-01-30 03:22

    I can't offer you a comprehensive set of best practices, but I can offer what I've learned so far:

    Managing your cache is a good idea. My app's cache is such that I know that I'll never need more than a certain number of cached files, so whenever I insert a new file into the cache, I delete the oldest files until I'm under the limit I have set. You could do something similar based on size, or simply age.

    Caching to the SD card, if it's available, is a good idea if your cache needs to take up a lot of space. You'll need to manage that space just as carefully, since it won't automatically clear that space for you. If you're caching image files, be sure to put them in a directory that begins with a dot, like "/yourAppHere/.cache". This will keep the images from showing up in the gallery, which is really annoying.

    Letting the user choose the location of the cache seems like overkill to me, but if your audience is very geeky, it might be appreciated.

    I haven't noticed a much of a penalty when caching to the SD, but I don't know how your app uses that information.

    0 讨论(0)
  • 2021-01-30 03:22

    Everyone has good ideas. I like the idea of using SoftReference's, although I'm not sure how often those get cleaned up, as this varies so much from VM to VM. You might want to combine that with regular HashMap to prevent you entire cache getting cleared every few minutes.

    EclipseLink has a few different cache implementations and pretty good documentation on them. You could probably take advantage of a few ideas from the implementation (e.g., LRU, MRU, etc.). e.g.,

    • hard cache
    • soft cache
    • combined hard/soft cache

    Since you're tuning a cache down to the nitty-gritty, I would recommend tuning it to different devices based on the hard specs. This is normally bad design, but the scope of the hardware that your software runs on mandates it, IMHO. e.g.,

    • Detect the amount of available memory on the SD card. Most new smart phones come with multi-GB SD cards, and those are pretty hard to fill up with regular usage for most users. Use away! You can also detect the amount of space available on the SD card on startup, and increase/decrease the size of your cache on startup.
    • Detect the amount of available memory and configure your caches with that in mind. If a user is using a hardware-intensive application, I don't think they'll mind that it makes up 200MB of RAM and provides a very fast user experience, especially since they spent a lot of money to have a phone that has 1-2GB RAM.

    Good luck!

    0 讨论(0)
提交回复
热议问题