<!--
{
  "documentType" : "article",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/accessing-cached-data",
  "metadataVersion" : "0.1.0",
  "role" : "article",
  "title" : "Accessing cached data"
}
-->

# Accessing cached data

Control how URL requests make use of previously cached data.

## Discussion

The URL Loading System caches responses both in memory and on disk, improving performance and reducing network traffic.

The [`URLCache`](/documentation/Foundation/URLCache) class is used for caching responses from network resources. Your app can directly access the shared cache instance by using the [`shared`](/documentation/Foundation/URLCache/shared) property of `URLCache`. Or, you can create your own caches for different purposes, setting distinct caches on your [`URLSessionConfiguration`](/documentation/Foundation/URLSessionConfiguration) objects.

### Set a cache policy for URL requests

Each [`URLRequest`](/documentation/Foundation/URLRequest) instance contains a [`URLRequest.CachePolicy`](/documentation/Foundation/URLRequest/CachePolicy-swift.typealias) object to indicate if and how caching should be performed.  You can change this policy to control caching for the request.

For convenience, [`URLSessionConfiguration`](/documentation/Foundation/URLSessionConfiguration) has a property called [`requestCachePolicy`](/documentation/Foundation/URLSessionConfiguration/requestCachePolicy); all requests created from sessions that use this configuration inherit their cache policy from the configuration.

The behaviors of the various policies are described in <doc:accessing-cached-data#Table-1>. This table shows the policies’ respective preferences for loading from cache or from the originating source, like a server or the local file system. Currently, only HTTP and HTTPS responses are cached. For FTP and file URLs, the only effect of a policy is to determine whether the request is allowed to access the originating source.

|Cache policy                                                                                                            |Local cache         |Originating source     |
|------------------------------------------------------------------------------------------------------------------------|--------------------|-----------------------|
|``doc://com.apple.foundation/documentation/Foundation/NSURLRequest/CachePolicy-swift.enum/reloadIgnoringLocalCacheData``|Ignored             |Accessed exclusively   |
|``doc://com.apple.foundation/documentation/Foundation/NSURLRequest/CachePolicy-swift.enum/returnCacheDataDontLoad``     |Accessed exclusively|Ignored                |
|``doc://com.apple.foundation/documentation/Foundation/NSURLRequest/CachePolicy-swift.enum/returnCacheDataElseLoad``     |Tried first         |Accessed only if needed|
|``doc://com.apple.foundation/documentation/Foundation/NSURLRequest/CachePolicy-swift.enum/useProtocolCachePolicy``      |Depends on protocol |Depends on protocol    |

For an explanation of how `useProtocolCachePolicy` is implemented for HTTP and HTTPS, see [`NSURLRequest.CachePolicy`](/documentation/Foundation/NSURLRequest/CachePolicy-swift.enum).  `useProtocolCachePolicy` is the default value for a `URLRequest` object.

> Note:
> `useProtocolCachePolicy` caches HTTPS responses to disk, which may be undesirable for securing user data. You can change this behavior by manually handling caching behavior, as described in <doc://com.apple.foundation/documentation/Foundation/accessing-cached-data#Manage-caching-programmatically>.

### Access the cache directly

You can get or set the cache object used by a `URLSession` object by using the [`urlCache`](/documentation/Foundation/URLSessionConfiguration/urlCache) property of the session’s [`configuration`](/documentation/Foundation/URLSession/configuration) object.

To look for the cached response to a given request, call [`cachedResponse(for:)`](/documentation/Foundation/URLCache/cachedResponse(for:)) on the cache. If cached data exists for the request, this call returns a [`CachedURLResponse`](/documentation/Foundation/CachedURLResponse) object; otherwise, it returns `nil`.

You can inspect resources used by the cache. The properties [`currentDiskUsage`](/documentation/Foundation/URLCache/currentDiskUsage) and [`diskCapacity`](/documentation/Foundation/URLCache/diskCapacity) represent the file system resources used by the cache, and [`currentMemoryUsage`](/documentation/Foundation/URLCache/currentMemoryUsage) and [`memoryCapacity`](/documentation/Foundation/URLCache/memoryCapacity) represent memory use.

You can remove cached data for individual items with [`removeCachedResponse(for:)`](/documentation/Foundation/URLCache/removeCachedResponse(for:)-1dh89). You can also clear out many cached items simultaneously with [`removeCachedResponses(since:)`](/documentation/Foundation/URLCache/removeCachedResponses(since:)), which removes cached items past a given date, or [`removeAllCachedResponses()`](/documentation/Foundation/URLCache/removeAllCachedResponses()), which wipes the entire cache.

### Manage caching programmatically

You can write to the cache programmatically, with the [`storeCachedResponse(_:for:)`](/documentation/Foundation/URLCache/storeCachedResponse(_:for:)-7p7bl) method, passing in a new `CachedURLResponse` object and a `URLRequest` object.

Typically, you manage the caching of a response while it’s being handled by a `URLSessionTask` object. To manage caching on a per-response basis, implement the [`urlSession(_:dataTask:willCacheResponse:completionHandler:)`](/documentation/Foundation/URLSessionDataDelegate/urlSession(_:dataTask:willCacheResponse:completionHandler:)) method of the [`URLSessionDataDelegate`](/documentation/Foundation/URLSessionDataDelegate) protocol. Note that this delegate method is called only for uploads and data tasks, and is not called for sessions with a background or ephemeral configuration.

The delegate receives two parameters: a `CachedURLResponse` object and a completion handler. Your delegate *must* call this completion handler directly, passing in one of the following:

- The provided `CachedURLResponse` object, to cache the proposed response as-is
- `nil`, to prevent caching
- A newly created `CachedURLResponse` object, typically based on the provided object, but with a modified [`storagePolicy`](/documentation/Foundation/CachedURLResponse/storagePolicy) and [`userInfo`](/documentation/Foundation/CachedURLResponse/userInfo) dictionary, as you see fit

The following example shows an implementation of `urlSession(_:dataTask:willCacheResponse:completionHandler:)`, which intercepts responses to HTTPS requests and allows the responses to be stored in the in-memory cache only.

Handling the urlSession(_:dataTask:willCacheResponse:completionHandler:) callback

```swift
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask,
                willCacheResponse proposedResponse: CachedURLResponse,
                completionHandler: @escaping (CachedURLResponse?) -> Void) {
    if proposedResponse.response.url?.scheme == "https" {
        let updatedResponse = CachedURLResponse(response: proposedResponse.response,
                                                data: proposedResponse.data,
                                                userInfo: proposedResponse.userInfo,
                                                storagePolicy: .allowedInMemoryOnly)
        completionHandler(updatedResponse)
    } else {
        completionHandler(proposedResponse)
    }
}
```

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)