Class

URLCache

The URLCache 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.

Overview

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.

Nested Types

NSURLCacheStoragePolicy

These constants specify the caching strategy used by an NSCachedURLResponse object.

Symbols

Creating a new cache object

init(memoryCapacity: Int, diskCapacity: Int, diskPath: String?)

Initializes an NSURLCache object with the specified values.

Getting and storing cached objects

func cachedResponse(for: URLRequest)

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

func storeCachedResponse(CachedURLResponse, for: URLRequest)

Stores a cached URL response for a specified request

Removing cached objects

func removeAllCachedResponses()

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

func removeCachedResponse(for: URLRequest)

Removes the cached URL response for a specified URL request.

Getting and setting on-disk cache properties

var currentDiskUsage: Int

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

var diskCapacity: Int

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

Getting and setting in-memory cache properties

var currentMemoryUsage: Int

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

var memoryCapacity: Int

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

Relationships

Inherits From

Conforms To