Class

URLCache

The URLCache class implements the caching of responses to URL load requests by mapping NSURLRequest objects to NSCached​URLResponse 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 cached​Response(for:​) and store​Cached​Response(_:​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

URLCache.Storage​Policy

These constants specify the caching strategy used by an NSCached​URLResponse object.

Symbols

Getting and setting shared cache

class var shared:​ URLCache

Returns the shared NSURLCache instance.

Creating a new cache object

init(memory​Capacity:​ Int, disk​Capacity:​ Int, disk​Path:​ String?)

Initializes an NSURLCache object with the specified values.

Getting and storing cached objects

func cached​Response(for:​ URLRequest)

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

func store​Cached​Response(Cached​URLResponse, for:​ URLRequest)

Stores a cached URL response for a specified request

Removing cached objects

func remove​All​Cached​Responses()

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

func remove​Cached​Response(for:​ URLRequest)

Removes the cached URL response for a specified URL request.

Getting and setting on-disk cache properties

var current​Disk​Usage:​ Int

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

var disk​Capacity:​ Int

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

Getting and setting in-memory cache properties

var current​Memory​Usage:​ Int

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

var memory​Capacity:​ Int

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

Relationships

Inherits From