<!--
{
  "availability" : [
    "iOS: 2.0.0 -",
    "iPadOS: 2.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.2.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/URLCache",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSURLCache"
  },
  "title" : "URLCache"
}
-->

# URLCache

An object that maps URL requests to cached response objects.

```
class URLCache
```

## Overview

The [`URLCache`](/documentation/Foundation/URLCache) class implements the caching of responses to URL load requests, by mapping [`NSURLRequest`](/documentation/Foundation/NSURLRequest) objects to [`CachedURLResponse`](/documentation/Foundation/CachedURLResponse) objects. It provides a composite in-memory and on-disk cache, and lets you manipulate the sizes of both the in-memory and on-disk portions. You can also control the path where cache data is persistently stored.

> Note:
> In iOS, the on-disk cache may be purged when the system runs low on disk space, but only when your app is not running.

### Thread safety

In iOS 8 and later, and macOS 10.10 and later, [`URLCache`](/documentation/Foundation/URLCache) is thread safe.

Although [`URLCache`](/documentation/Foundation/URLCache) instance methods can safely be called from multiple execution contexts at the same time, be aware that methods like  [`cachedResponse(for:)`](/documentation/Foundation/URLCache/cachedResponse(for:)) and [`storeCachedResponse(_:for:)`](/documentation/Foundation/URLCache/storeCachedResponse(_:for:)-7p7bl) have an unavoidable race condition when attempting to read or write responses for the same request.

Subclasses of [`URLCache`](/documentation/Foundation/URLCache) must implement overridden methods in such a thread-safe manner.

### Subclassing notes

The [`URLCache`](/documentation/Foundation/URLCache) class is meant to be used as-is, but you can subclass it when you have specific needs. For example, you might want to screen which responses are cached, or reimplement the storage mechanism for security or other reasons.

When overriding methods of this class, be aware that methods that take a `task` parameter are preferred by the system to those that do not. Therefore, you should override the task-based methods when subclassing, as follows:

- Storing responses in the cache — Override the task-based ``doc://com.apple.foundation/documentation/Foundation/URLCache/storeCachedResponse(_:for:)-8uq91``, instead of or in addition to the request-based ``doc://com.apple.foundation/documentation/Foundation/URLCache/storeCachedResponse(_:for:)-7p7bl``.
- Getting responses from the cache — Override ``doc://com.apple.foundation/documentation/Foundation/URLCache/getCachedResponse(for:completionHandler:)``, instead of or in addition to ``doc://com.apple.foundation/documentation/Foundation/URLCache/cachedResponse(for:)``.
- Removing cached responses — Override the task-based ``doc://com.apple.foundation/documentation/Foundation/URLCache/removeCachedResponse(for:)-1zwp6``, instead of or in addition to the request-based ``doc://com.apple.foundation/documentation/Foundation/URLCache/removeCachedResponse(for:)-1dh89``.

## Topics

### Getting and setting shared cache

[`shared`](/documentation/Foundation/URLCache/shared)

The shared URL cache instance.

### Creating a new cache object

[`init(memoryCapacity:diskCapacity:directory:)`](/documentation/Foundation/URLCache/init(memoryCapacity:diskCapacity:directory:))

Creates a URL cache object with the specified memory and disk capacities, in the specified directory.

[`initWithMemoryCapacity:diskCapacity:directoryURL:`](/documentation/Foundation/NSURLCache/initWithMemoryCapacity:diskCapacity:directoryURL:)

Creates a URL cache object with the specified memory and disk capacities, in the specified directory.

[`init(memoryCapacity:diskCapacity:diskPath:)`](/documentation/Foundation/URLCache/init(memoryCapacity:diskCapacity:diskPath:))

Creates a URL cache object with the specified values.

### Getting and storing cached objects

[`cachedResponse(for:)`](/documentation/Foundation/URLCache/cachedResponse(for:))

Returns the cached URL response in the cache for the specified URL request.

[`storeCachedResponse(_:for:)`](/documentation/Foundation/URLCache/storeCachedResponse(_:for:)-7p7bl)

Stores a cached URL response for a specified request.

[`getCachedResponse(for:completionHandler:)`](/documentation/Foundation/URLCache/getCachedResponse(for:completionHandler:))

Gets the cached URL response for a data task, passing it to the provided completion handler.

[`storeCachedResponse(_:for:)`](/documentation/Foundation/URLCache/storeCachedResponse(_:for:)-8uq91)

Stores a cached URL response for a specified data task.

### Removing cached objects

[`removeCachedResponse(for:)`](/documentation/Foundation/URLCache/removeCachedResponse(for:)-1dh89)

Removes the cached URL response for a specified URL request.

[`removeCachedResponse(for:)`](/documentation/Foundation/URLCache/removeCachedResponse(for:)-1zwp6)

Removes the cached URL response for a specified data task.

[`removeCachedResponses(since:)`](/documentation/Foundation/URLCache/removeCachedResponses(since:))

Clears the given cache of any cached responses since the provided date.

[`removeAllCachedResponses()`](/documentation/Foundation/URLCache/removeAllCachedResponses())

Clears the receiver’s cache, removing all stored cached URL responses.

### Getting and setting on-disk cache properties

[`currentDiskUsage`](/documentation/Foundation/URLCache/currentDiskUsage)

The current size of the on-disk cache, in bytes.

[`diskCapacity`](/documentation/Foundation/URLCache/diskCapacity)

The capacity of the on-disk cache, in bytes.

### Getting and setting in-memory cache properties

[`currentMemoryUsage`](/documentation/Foundation/URLCache/currentMemoryUsage)

The current size of the in-memory cache, in bytes.

[`memoryCapacity`](/documentation/Foundation/URLCache/memoryCapacity)

The capacity of the in-memory cache, in bytes.

### Cache storage policies

[`URLCache.StoragePolicy`](/documentation/Foundation/URLCache/StoragePolicy)

These constants specify the caching strategy used by an [`CachedURLResponse`](/documentation/Foundation/CachedURLResponse) object.



---

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)
