5

Now, I am developing a news reader app like BBC news iOS. see in BBC News

In my app, I must download image from server to and show it in view to make users easier to choose the news they want to read.

For more performance, I must cache image to avoid reloading image for server. I know that there are 2 kinds of cache: In-memory cache that saving images in memory (RAM) and DiskCach that save images in Disk to load it when we need.

My question is: What is best images cache mixed strategies for my App? (use both in-memory cache and image-cache) My solution is:

  • download image --> save them in diskcache + save them in memory cache --> load image from in-memory cache on demand and show in view ---> in-memory cache over its MAX_SIZE --> free in-memory cache ---> load image from disk cache on demand and save it to memory cache --> repeat........

Is my solution is right approach?

Another question: when in-memory cache over its MAX_SIZE --> we will free its --> all images in cache will lose so image in our view will disappear. --> How to solve this problem?

Sorry for poor English. Thank in advance.

hahv
  • 582
  • 1
  • 4
  • 16

1 Answers1

3

In one of my projects I implemented pretty much the same caching methods (Disk Cache and Memory Cache).

Maximum cache size

Each cache system had its own max size limit. The "size" of each image was computed differently in the cache systems.

For the memory cache, each image would have a size computed as image size = image width * image height (in pixels) So, the maximum size for the memory cache would represent a the maximum area of a pixel surface

For the disk cache, I used the actual file size for each file.

Making room

When using the cache systems, you might get to a situation where one of the caches is full and you want to insert a new item in it - you have to remove some items to make room.

What I did was assign a timestamp to each entry in the cache. Every time I access that item I updated the timestamp. When you want to make room, you just need to start removing items from the oldest to the newest based on the last access timestamp.

This is a simple algorithm for freeing up space and in some cases might actually behave poorly. It is up to you to experiment and see if you need something more advanced than this. For example, you could improve this method by adding a priority value for each item and keep old items in the cache if their priority is high. Again, it depends on your app's needs.

Expiration

For the disk cache, I would definitely add an expiration date for each entry. If the memory cache is destroyed when the user completely terminates the app, images in the disk cache might be stuck in there forever.

Encapsulation

Another aspect I would consider, is making the caching system as transparent as possible to the programmer. If you want to enable/disable one of the cache it would be best to have most of the code remain the same.

In my app, I built a central content delivery system and I would always request images from the internet through this object. The caching system would then check the local caches (memory / disk) and either return me the image immediately or make a request to download it.

Either way... I, as the "user" of the caching system did not care what was happening behind the curtains. All I knew is I made a request to get an image from an URL and I got it (faster or slower depending if the image was cached).

Andrei Stanescu
  • 6,353
  • 4
  • 35
  • 64