In a city-guide app whose disk cache lives in the OS's purgeable cache folder, what must the design guarantee for the morning the folder is found empty?
answer
- the folder is the OS's to empty
- no promised schedule
- a missing file is a miss
- the index outlives nothing
- never the only copy
basics
~20 sEvery disk-cache read must be optional: a missing file is a miss, the cache's index and byte total are rebuilt rather than trusted, and nothing the user made, or the app promised offline, lives only in that folder.
solid answer
~40 sBoth native platforms let the OS delete an app's cache files when storage runs low, and neither says when, so I design for any entry vanishing between two launches. Every disk read is optional: a missing or unreadable file is a miss that falls through to the network or the offline state. The cache's index must not outlive its files, so I keep it inside the same folder or verify the file on each read, and I recompute the byte total at launch instead of trusting a saved counter. Nothing there is the only copy: saved lists and the user's notes live in the persistent store. And if the product promises that saved venues open offline, those pages go in app-managed storage under the app's own ceiling, not in the purgeable folder.
go deeper
Recall that the OS may delete an app's cache folder when storage runs low, so the app must cope with any cached file being missing.
Explain the read path that tolerates a purge: a missing or unreadable file is a miss, and the next step is the network or an offline state, never an error screen.
Show the consistency work: an index that cannot point at deleted files, a byte total recomputed at launch, offline promises backed by app-managed storage, and backup exclusion for re-downloadable copies.
Set the rule across the app's features: what may be purgeable, what the product may promise offline, and who owns the storage ceiling for anything kept outside the cache folder.
A disk cache usually belongs in the folder the OS provides for caches, because that is where the OS expects disposable data: it is left out of device backups, and the OS can reclaim it when the phone runs out of space. The price of that convenience is that the folder is the OS's to empty. An app designed as if the folder were permanent works for weeks and then fails on the morning after the phone ran short of storage. ## What the two platforms actually promise - **One platform** documents that the system may delete the cache folder on rare occasions when the device is very low on disk space, that this never happens while the app is running, that restoring from a backup is not the only other case, and that the folder's contents are not backed up; its backup guidance adds that the system purges such folders periodically, without saying when. - **The other platform** documents that when internal storage is low the system may delete the app's cache files, tells apps to check that a cache file exists before reading it, and warns them not to rely on the system to clean the folder for them. Neither promises **when**. One speaks of the whole folder, the other of cache files, so the safe design assumes any subset of entries can vanish between two reads, and that an entry written yesterday may be gone today. ## The guarantees the design needs 1. **Every disk-cache read is optional.** A missing file is a miss: fall through to the network, or show the offline state. An unreadable or half-written file is also a miss, deleted on the spot. 2. **The index never outlives the files.** If the cache keeps an index (key, file name, size, last access), either keep it inside the same folder so both vanish together, or verify the file on each read and drop rows whose file is gone. 3. **The byte total is recomputed**, at launch or lazily, rather than trusted from a stored counter. A counter saved before the purge says the cache is full when the folder is empty, so the app evicts for nothing or refuses to cache. 4. **Nothing there is the only copy.** The user's saved lists, notes on a venue and any unsent edits live in the persistent store outside this folder; deciding that store is a separate design choice. 5. **Offline promises are backed by storage the app manages.** If the product says saved venues open offline, the venue pages behind that promise cannot sit only in the purgeable folder: keep them in app-managed storage counted against the app's own ceiling, or do not make the promise. 6. **An empty cache on a cold, offline start is a designed state**: saved-list titles still show from the store, and venue screens say they need a connection instead of spinning. ## Where the index lives | Index location | After the OS empties the folder | Verdict | |---|---|---| | Inside the cache folder | gone with the files; the cache restarts empty and consistent | simplest and safe | | In the persistent store | rows point at files that no longer exist | workable only if every read verifies the file and launch reconciles the total | | No index (scan the folder) | consistent by construction | costs a directory scan to size or evict | ## Backup and storage Purgeable folders are excluded from device backup on one platform, which is right for a cache. Offline copies moved out of that folder to keep a promise are then backed up unless the app marks them as excluded; data the app can download again usually should be, so a restored phone fetches it rather than carrying it in the backup. The app's own ceiling and maximum age still apply to those files, because the OS will not reclaim them. ## Mistakes interviewers listen for - **"The OS never touches the cache folder"**, which turns a storage-pressure purge into lost user data. - **"The OS clears it every few days"**, inventing a schedule neither platform promises and building cleanup on it. - **Showing an error on a missing file** instead of treating it as a miss. - **Trusting the index after a relaunch** without checking that its files still exist. - **Keeping saved venues in the cache folder** because the user chose them; choosing them is exactly why they must not be disposable. ## Where this question stops - Choosing the persistent store and what deserves to be in it is the store-choice part of the design. - Whether a surviving entry is fresh enough to show is the staleness policy. - One framework's API for locating the cache folder belongs to that framework's own material.
- Can the OS empty the cache folder while the app is running?One platform documents that it does not; the other only says cache files may be deleted when storage is low and that apps should check a file exists before reading it. Design for the weaker promise: any read may miss, and an entry written earlier may be gone by the next launch or the next read.
- Should the app delete its own cache files rather than leave that to the OS?Yes. The OS reclaims the folder only under storage pressure and on its own timing, and one platform's guide says plainly not to rely on it. The app's byte ceiling and maximum age keep the cache from being the reason the phone runs out of space in the first place.
- The OS never touches files in the app's cache folder.
- The OS clears the cache folder every few days, so it stays small by itself.
- A missing cache file means storage is corrupt, so the app should show an error.
- The cache index can be trusted after a relaunch without checking its files.
- Saved venues are safe in the cache folder because the user chose them.