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.

Topics

Getting and setting shared cache

sharedURLCache

Returns the shared NSURLCache instance.

Creating a new cache object

initWithMemoryCapacity:diskCapacity:diskPath:

Initializes an NSURLCache 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

Removing cached objects

removeCachedResponseForRequest:

Removes the cached URL response for a specified URL request.

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 receiver’s on-disk cache, in bytes.

diskCapacity

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

Getting and setting in-memory cache properties

currentMemoryUsage

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

memoryCapacity

The capacity of the receiver’s 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.