Nuke Documentation

Class Data​Cache

public final class DataCache: DataCaching  

An LRU disk cache that stores data in separate files.

The DataCache uses LRU cleanup policy (least recently used items are removed first). The elements stored in the cache are automatically discarded if either cost or count limit is reached. The sweeps are performed periodically.

DataCache always writes and removes data asynchronously. It also allows for reading and writing data in parallel. This is implemented using a "staging" area which stores changes until they are flushed to disk:

// Schedules data to be written asynchronously and returns immediately
cache[key] = data

// The data is returned from the staging area
let data = cache[key]

// Schedules data to be removed asynchronously and returns immediately
cache[key] = nil

// Data is nil
let data = cache[key]

Thread-safe.

DataCache DataCache DataCaching DataCaching DataCache->DataCaching

Conforms To

DataCaching

Data cache.

Nested Type Aliases

Key

public typealias Key = String

A cache key.

Filename​Generator

public typealias FilenameGenerator = (_ key: String) -> String? 

A function which generates a filename for the given key. A good candidate for a filename generator is a cryptographic hash function like SHA1.

The reason why filename needs to be generated in the first place is that filesystems have a size limit for filenames (e.g. 255 UTF-8 characters in AFPS) and do not allow certain characters to be used in filenames.

Initializers

init(name:​filename​Generator:​)

public convenience init(name: String, filenameGenerator: @escaping (String) -> String? = DataCache.filename(for:)) throws  

Creates a cache instance with a given name. The cache creates a directory with the given name in a .cachesDirectory in .userDomainMask.

Parameters

filename​Generator @escaping (String) -> String?

Generates a filename for the given URL. The default implementation generates a filename using SHA1 hash function.

init(path:​filename​Generator:​)

public init(path: URL, filenameGenerator: @escaping (String) -> String? = DataCache.filename(for:)) throws  

Creates a cache instance with a given path.

Parameters

filename​Generator @escaping (String) -> String?

Generates a filename for the given URL. The default implementation generates a filename using SHA1 hash function.

Properties

size​Limit

public var sizeLimit: Int = 1024 * 1024 * 150

Size limit in bytes. 150 Mb by default.

Changes to sizeLimit will take effect when the next LRU sweep is run.

path

public let path: URL

The path for the directory managed by the cache.

sweep​Interval

public var sweepInterval: TimeInterval = 30

The number of seconds between each LRU sweep. 30 by default. The first sweep is performed right after the cache is initialized.

Sweeps are performed in a background and can be performed in parallel with reading.

queue

public let queue  

A queue which is used for disk I/O.

total​Count

public var totalCount: Int  

The total number of items in the cache.

total​Size

public var totalSize: Int  

The total file size of items written on disk.

Uses URLResourceKey.fileSizeKey to calculate the size of each entry. The total allocated size (see totalAllocatedSize. on disk might actually be bigger.

total​Allocated​Size

public var totalAllocatedSize: Int  

The total file allocated size of all the items written on disk.

Uses URLResourceKey.totalFileAllocatedSizeKey.

Methods

filename(for:​)

public static func filename(for key: String) -> String?  

A FilenameGenerator implementation which uses SHA1 hash function to generate a filename from the given key.

cached​Data(for:​)

public func cachedData(for key: Key) -> Data?  

Retrieves data for the given key.

contains​Data(for:​)

public func containsData(for key: String) -> Bool  

Returns true if the cache contains the data for the given key.

store​Data(_:​for:​)

public func storeData(_ data: Data, for key: Key)  

Stores data for the given key. The method returns instantly and the data is written asynchronously.

remove​Data(for:​)

public func removeData(for key: Key)  

Removes data for the given key. The method returns instantly, the data is removed asynchronously.

remove​All()

public func removeAll()  

Removes all items. The method returns instantly, the data is removed asynchronously.

filename(for:​)

public func filename(for key: Key) -> String?  

Uses the FilenameGenerator that the cache was initialized with to generate and return a filename for the given key.

url(for:​)

public func url(for key: Key) -> URL?  

Returns url for the given cache key.

flush()

public func flush()  

Synchronously waits on the caller's thread until all outstanding disk I/O operations are finished.

flush(for:​)

public func flush(for key: Key)  

Synchronously waits on the caller's thread until all outstanding disk I/O operations for the given key are finished.

sweep()

public func sweep()  

Synchronously performs a cache sweep and removes the least recently items which no longer fit in cache.