Loadability Documentation Beta

Class Serializable​Cache

public final class SerializableCache<Key: Codable & Hashable & Identifiable, Value: Codable>: Cache<Key, Value>, Codable

A mutable collection, with support for serialization and storage, used to store key-value pairs that are subject to eviction when resources are low.

%3 SerializableCache SerializableCache Cache<Key, Value> Cache<Key, Value> SerializableCache->Cache<Key, Value> Codable Codable SerializableCache->Codable

Conforms To

Cache<Key, Value>
Codable

Initializers

init(from:​)

public required convenience init(from decoder: Decoder) throws

Decodes an encoded SerializableCache.

Parameters

decoder Decoder

The decoder.

Throws

When there is an error in decoding.

Methods

encode(to:​)

public final func encode(to encoder: Encoder) throws

Encodes the cache and key-value pairs to disk.

Parameters

encoder Encoder

The encoder.

Throws

When there is an error in encoding.

save()

public final func save()

Encodes and saves the cache to disk.

load(name:​should​Automatically​Remove​Stale​Items:​folder​URL:​)

public static func load(name: String, shouldAutomaticallyRemoveStaleItems: Bool = false, folderURL: URL? = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first) -> SerializableCache<Key, Value>

Loads a previously-serialized cache from disk.

Parameters

name String

The unique name of the cache.

should​Automatically​Remove​Stale​Items Bool

Whether to automatically remove stale items, defaults to false.

folder​URL URL?

The folder in which to store the cache, defaults to the system cache directory.

Returns

The loaded cache.