---
title: URLCache
framework: foundation
role: symbol
role_heading: Class
path: foundation/urlcache
---

# URLCache

An object that maps URL requests to cached response objects.

## Declaration

```swift
class URLCache
```

## Mentioned in

Accessing cached data

## Overview

Overview The URLCache class implements the caching of responses to URL load requests, by mapping NSURLRequest objects to 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 is thread safe. Although URLCache instance methods can safely be called from multiple execution contexts at the same time, be aware that methods like  cachedResponse(for:) and storeCachedResponse(_:for:) have an unavoidable race condition when attempting to read or write responses for the same request. Subclasses of URLCache must implement overridden methods in such a thread-safe manner. Subclassing notes The 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 storeCachedResponse(_:for:), instead of or in addition to the request-based storeCachedResponse(_:for:). Getting responses from the cache — Override getCachedResponse(for:completionHandler:), instead of or in addition to cachedResponse(for:). Removing cached responses — Override the task-based removeCachedResponse(for:), instead of or in addition to the request-based removeCachedResponse(for:).

## Topics

### Getting and setting shared cache

- [shared](foundation/urlcache/shared.md)

### Creating a new cache object

- [init(memoryCapacity:diskCapacity:directory:)](foundation/urlcache/init(memorycapacity:diskcapacity:directory:).md)
- [init(memoryCapacity:diskCapacity:diskPath:)](foundation/urlcache/init(memorycapacity:diskcapacity:diskpath:).md)

### Getting and storing cached objects

- [cachedResponse(for:)](foundation/urlcache/cachedresponse(for:).md)
- [storeCachedResponse(_:for:)](foundation/urlcache/storecachedresponse(_:for:)-7p7bl.md)
- [getCachedResponse(for:completionHandler:)](foundation/urlcache/getcachedresponse(for:completionhandler:).md)
- [storeCachedResponse(_:for:)](foundation/urlcache/storecachedresponse(_:for:)-8uq91.md)

### Removing cached objects

- [removeCachedResponse(for:)](foundation/urlcache/removecachedresponse(for:)-1dh89.md)
- [removeCachedResponse(for:)](foundation/urlcache/removecachedresponse(for:)-1zwp6.md)
- [removeCachedResponses(since:)](foundation/urlcache/removecachedresponses(since:).md)
- [removeAllCachedResponses()](foundation/urlcache/removeallcachedresponses().md)

### Getting and setting on-disk cache properties

- [currentDiskUsage](foundation/urlcache/currentdiskusage.md)
- [diskCapacity](foundation/urlcache/diskcapacity.md)

### Getting and setting in-memory cache properties

- [currentMemoryUsage](foundation/urlcache/currentmemoryusage.md)
- [memoryCapacity](foundation/urlcache/memorycapacity.md)

### Cache storage policies

- [URLCache.StoragePolicy](foundation/urlcache/storagepolicy.md)

## Relationships

### Inherits From

- [NSObject](objectivec/nsobject-swift.class.md)

### Conforms To

- [CVarArg](swift/cvararg.md)
- [CustomDebugStringConvertible](swift/customdebugstringconvertible.md)
- [CustomStringConvertible](swift/customstringconvertible.md)
- [Equatable](swift/equatable.md)
- [Hashable](swift/hashable.md)
- [NSObjectProtocol](objectivec/nsobjectprotocol.md)
- [Sendable](swift/sendable.md)
- [SendableMetatype](swift/sendablemetatype.md)

## See Also

### Cache behavior

- [Accessing cached data](foundation/accessing-cached-data.md)
- [CachedURLResponse](foundation/cachedurlresponse.md)
