Class

NSURLCache

An object that maps URL requests to cached response objects.

Overview

The NSURLCache class implements the caching of responses to URL load requests by mapping NSURLRequest objects to NSCachedURLResponse 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 stored persistently.

Thread Safety

In iOS 8 and later, and macOS 10.10 and later, NSURLCache is thread safe.

Although NSURLCache instance methods can safely be called from multiple execution contexts at the same time, be aware that methods like cachedResponseForRequest: and storeCachedResponse:forRequest: have an unavoidable race condition when attempting to read or write responses for the same request.

Subclasses of NSURLCache must implement overridden methods in such a thread safe manner.

Subclassing Notes

The NSURLCache class is meant to be used as-is, but you can subclass it if 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:

Topics

Getting and Setting Shared Cache

sharedURLCache

The shared URL cache instance.

Creating a New Cache Object

initWithMemoryCapacity:diskCapacity:diskPath:

Creates an URL cache object with the specified values.

Getting and Storing Cached Objects

cachedResponseForRequest:

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

storeCachedResponse:forRequest:

Stores a cached URL response for a specified request.

getCachedResponseForDataTask:completionHandler:

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

storeCachedResponse:forDataTask:

Stores a cached URL response for a specified data task.

Removing Cached Objects

removeCachedResponseForRequest:

Removes the cached URL response for a specified URL request.

removeCachedResponseForDataTask:

Removes the cached URL response for a specified data task.

removeCachedResponsesSinceDate:

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

removeAllCachedResponses

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

Getting and Setting On-disk Cache Properties

currentDiskUsage

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

diskCapacity

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

Getting and Setting In-memory Cache Properties

currentMemoryUsage

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

memoryCapacity

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

Relationships

Inherits From

See Also

Cache Behavior

Accessing Cached Data

Control how URL requests make use of previously cached data.

NSCachedURLResponse

A cached response to a URL request.