Class
SerializableCache
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.
Relationships
Conforms To
Cache<Key, Value>
Codable
Initializers
init(from:)
public required convenience init(from decoder: Decoder) throws
Decodes an encoded SerializableCache
.
Parameters
Name | Type | Description |
---|---|---|
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
Name | Type | Description |
---|---|---|
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:shouldAutomaticallyRemoveStaleItems:folderURL:)
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 | Type | Description |
---|---|---|
name | String |
The unique name of the cache. |
shouldAutomaticallyRemoveStaleItems | Bool |
Whether to automatically remove stale items, defaults to |
folderURL | URL? |
The folder in which to store the cache, defaults to the system cache directory. |
Returns
The loaded cache.