views:

118

answers:

1

I have a fairly image-intensive iPhone app, and I'm looking to store remotely downloaded images locally in the app's sandbox tmp directory to avoid unnecessary network requests. Is there a limit to the total size of the files stored in an app's directories, or does the app need to manage that? How would the app determine the size of the files in the tmp directory?

Also, if the app needs to manage the size of the cache, I'd like to implement some kind of cache policy to determine which files get invalidated. How would I go about doing this? If I want to implement a basic LRU caching policy - invalidating files that have been used least recently - it seems like I would need to store access counts for each image and store that on the disk as well, which seems kind of funky. I suppose an easy size management policy would be to simply completely wipe the cache each time the application terminates.

Also, what's the difference between using the directory from NSCachesDirectory versus NSTemporaryDirectory? The Apple docs mention both, but don't talk about which one to use for what type of files. I'm thinking the NSTemporaryDirectory is more like a Unix /var/tmp directory, and used for ephemeral data that can be wiped out at anytime. Seems to me the NSCachesDirectory is more appropriate for storing cached images, since the files could be needed across multiple app lifecycles.

+1  A: 

All temporary directories are local to your application; any of them will work and there is no artificial limit to the size of their contents.

A persistent LRU cache policy should be both sufficient and relatively easy to implement.

rpetrich
That's what I'm going with. For those that are curious - I'm also taking advantage of the NSFileModificationDate attribute for each file for enforcing LRU caching.
pmc255
`NSFileModificationDate` tracks when the file is modified rather than when it was last used. Normally one would use `stat` and the `st_atime` field to check when a file was last accessed, but iPhone mounts the user partition with the `noatime` parameter set so there's no built-in way to track this information. Roll your own :)
rpetrich