URLCache
Also known as: nsurlcache, http response cache ios
URLCache is Foundation's in-memory and on-disk cache for HTTP and HTTPS responses fetched via URLSession. It stores response data keyed by request so repeat network calls can be served locally. Apps can inspect its current size and clear it, which a storage cleaner uses to reclaim network cache.
- URLCache stores HTTP/HTTPS responses for URLSession, keyed by URLRequest.
- It enforces separate memoryCapacity and diskCapacity limits.
- removeAllCachedResponses() clears it; currentDiskUsage reports its size.
How URLCache works
URLCache (formerly NSURLCache) maps a URLRequest to a stored CachedURLResponse containing the response and its data. URLSession consults it automatically according to the request's cachePolicy and the server's HTTP caching headers, so frequently requested resources can be returned without a new round trip.
It maintains separate memoryCapacity and diskCapacity limits. The disk store lives inside the app's sandbox, typically under Library/Caches, which means it is purgeable and excluded from backups. Apps access the active cache through URLCache.shared or a custom instance assigned to a session configuration.
Inspecting and clearing it
An app can read URLCache.shared.currentDiskUsage and currentMemoryUsage to report how much network cache exists, then call removeAllCachedResponses() to clear everything or removeCachedResponse(for:) to drop a single entry. There is also removeCachedResponses(since:) to evict entries newer than a date.
For a cleaner, clearing URLCache complements wiping Library/Caches files directly: URLCache holds structured HTTP responses, while raw cache files may sit elsewhere in the same folder. Clearing it only affects the app's own responses; it cannot touch other apps' network caches.