I've already asked When exactly do things get removed from urlcache's memory and disk?
Now I have some more follow up questions:
The memory cache is restricted by the iPhone's ram (usually 2Gb). But the disk Persistence is limited by 64Gb or 128Gb. Is that correct?
Does it ever make sense to have a persistence more than your memory storage. Can it be useful if you don't want to have a high memory footprint (and don't want app to get terminated from its suspended state) ie allow the cache to be restored from disk storage and then return persisted results?
After control clicking on URLCache.shared
I see the following comments:
- Memory capacity: 4 megabytes (4 * 1024 * 1024 bytes)
- Disk capacity: 20 megabytes (20 * 1024 * 1024 bytes)
- Disk path:
(user home directory)/Library/Caches/(application bundle id)
Users who do not have special caching requirements or constraints should find the default shared cache instance acceptable. If this default shared cache instance is not acceptable,
+setSharedURLCache:
can be called to set a differentNSURLCache
instance to be returned from this method. Callers should take care to ensure that the setter is called at a time when no other caller has a reference to the previously-set shared URL cache. This is to prevent storing cache data from becoming unexpectedly unretrievable.
Or docs
The response size is small enough to reasonably fit within the cache. (For example, if you provide a disk cache, the response [data] must be no larger than about 5% of the disk cache size.)
I included data myself
So I'm thinking my rationale is right.
How does the overall process work of reading/writing/restoring of cache work?
I mean when I make a network request for the first time then is it that the entire response/error/data gets written/stored in cache and then into persistence?
Next time if I want read then it first starts from the cache then if the response was not stale/expired then it will return it. Nothing would change for disk storage.
If it was expired then it would make a new request and only upon getting a successful response, it would purge the response from memory and disk and write the new response into cache and disk. If the new request failed then it won't purge, rather it would just keep stale/expired data so if we wanted (to load expired response) it would load from there?
And when app is terminated the memory is flushed out. Disk storage is left intact unless device was low on memory or you've reached size limit. Upon next launch of app, the memory reloads whatever's in the disk storage into the cache.
This cache restoration will start loading the latest data is has stored then move onto the data that was older until it either reaches its size limit or just reach the end of the items stored on the disk. Right?
I'm asking all this because I want to improve the caching experience in an app and improve my overall understanding so I won't increase the memory usage* of the app too much so it won't get flushed out of the memory from its suspended state due my apps high memory usage or other apps being in need.
*: some of our network requests will be downloading thumbnails, so I need to be considerate when I'm increasing my caching size limit.
Disk cache memory: Instead of the CPU having to wait for data to be stored on a slow hard disk, it sends the information to be saved to the disk cache memory. From the disk cache, the data is written to the hard drive, which stores it permanently.
Since Finder and iTunes categorize cached files as Other, reported usage for Music or Videos might differ. To view usage on your device, go to Settings > General > [Device] Storage. Your device automatically deletes cached files and temporary files when it needs more space. You don't need to delete them yourself.
Disk cache uses iOS's file system to store data converted from object. Usually it will create its own directory in app's caches directory. A file will be created for every object.
If you want to get to know deep about memory management, you should dive into lower level APIs. URLSession, URLCache and etc. are very high level APIs. There are plenty of WWDC sessions out there about memory frames, Image caching, network caching and etc. Each part has a lot of explanation. I'm suggesting you to watch all WWDC videos (if you hadn't) for start and warm up. Some of theme has great explanation about the core concepts like this.
check out these two from this year WWDC:
Image and Graphics Best Practices
iOS Memory Deep Dive
We can sit here and discuss about your questions for weeks! But for quick answer:
Compressed data temporarily moved to disk to make room in memory for more recently used data
But memory has it own cache in its frames. Sometimes caching reduce performance instead of improving it. Because of it's frames.
Not always. By default, only success requests will cache (If server not requested the client to "don't cache it" in the header). But as you may know, URLSession has plenty of configuration for caching on disk, memory and etc. in a very high level API. See URLSession Documentation.
See NSURLRequestCachePolicy documentation. And this is a good tutorial about it. Based on the policy you chose, it behaves different and can purge the previous cache or keep it until next success.
The HomeDirectory contains some main directories. iOS behaves differently with each one. iOS will only clean the the tmp directory (where the disk cache lives). You can store cache in a different directory like Documents to prevent iOS from removing it. But the point is the meaning of the cache word itself.
Any less space or even more space from the actual need is critical. It can corrupt the process or waste the memory/disk. Remember the reason of link lists invention? You can have different caches for different purposes like images and JSONs. But the point about images is:
These are all at the heart of the Foundation framework and all known third parties are just wrappers around them. So the only benefits they have are: Predefined defaults based on the thousands of contributors knowledge and higher level APIs.
Alamofire and Kingfisher are great examples of them.
Conclusion
The right cache size depends on the use case. Depends on number of data variants, images count, size of each image, how often is the same image accessed and etc. for example if you are constantly changing the image in the cache, it could actually have an adverse effect on the battery life! The best way to decide on an appropriate cache size is to test. Run the app under Instruments to measure both performance and battery usage. Keep increasing the cache size until you can't discern a difference in performance. That's the largest size you'd need, at least under the test conditions. Then reduce the size until performance is just barely acceptable to determine the smallest acceptable size. The right size is somewhere between those two sizes, depending on what you think is important.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With