DiskCache
open class DiskCache
A specialized cache for storing data in disk. Based on Haneke Disk Cache and customized for the MapCache project.
-
Gets the root base folder to be used.
Declaration
Swift
open class func baseURL() -> URL
-
URL of the physical folder of the Cache in the file system.
Declaration
Swift
public let folderURL: URL
-
A shortcut for
folderURL.path
.Declaration
Swift
open var path: String { get }
-
Sum of the allocated size in disk for the cache expressed in bytes.
Note that, this size is the actual disk allocation. It is equivalent with the amount of bytes that would become available on the volume if the directory is deleted.
For example, a file may just contain 156 bytes of data (size listed in
ls
command), however its disk size is 4096, 1 volume block (as listed usingdu -h
)This size is calculated each time
Declaration
Swift
open var diskSize: UInt64
-
This is the sum of the data sizes of the files within the
DiskCache
This size is calculated each time it is used
Seealso
diskSize
Declaration
Swift
open var fileSize: UInt64? { get }
-
Maximum allowed cache disk allocated size for this
DiskCache
Defaults to unlimited capacity (
UINT64_MAX`)Declaration
Swift
open var capacity: UInt64 { get set }
-
Queue for making async operations.
Declaration
Swift
open lazy var cacheQueue: DispatchQueue { get set }
-
Constructor
Declaration
Swift
public init(withName cacheName: String, capacity: UInt64 = UINT64_MAX)
Parameters
withName
Name of the cache, will be the subfolder name too.
capacity
capacity of the cache in bytes. Defaults to virutally unlimited capacity (
UINT64_MAX
) -
Get the path for key.
Declaration
Swift
open func path(forKey key: String) -> String
-
Sets the data for the key asyncronously. Use this function for writing into the cache.
Declaration
Swift
open func setData(_ data: Data, forKey key: String)
-
Sets the data for the key synchronously.
Declaration
Swift
open func setDataSync(_ data: Data, forKey key: String)
-
Fetches the image data from storage synchronously.
Declaration
Swift
open func fetchDataSync(forKey key: String, failure fail: ((Error?) -> ())? = nil, success succeed: @escaping (Data) -> ())
Parameters
forKey
Key within the cache
failure
closure to be run in case of error
success
closure to be run once the data is ready
-
Removes asynchronously the data from the diskcache for the key passed as argument.
Declaration
Swift
open func removeData(withKey key: String)
Parameters
withKey
key to be removed
-
Removes asynchronously all data from the cache. Calls completition closure once the task is done.
Declaration
Swift
open func removeAllData(_ completion: (() -> ())? = nil)
Parameters
completition
closure run once all the files are deleted from the cache
-
Removes the cache from the system. This method not only removes the data, it also removes the cache folder.
Do not call any method after removing the cache, create a new instance instead.
Declaration
Swift
open func removeCache()
-
Asynchronously updates the access date of a file.
Declaration
Swift
open func updateAccessDate(_ getData: @autoclosure @escaping () -> Data?, key: String)
-
Calculates the size used by all the files in the cache.
Declaration
Swift
public func calculateDiskSize() -> UInt64
-
It checks if the capacity of the cache has been reached. If so, it removes the least recently used file (LRU).
Declaration
Swift
fileprivate func controlCapacity()
-
Updates the time a file was accessed for the last time.
Declaration
Swift
@discardableResult fileprivate func updateDiskAccessDate(atPath path: String) -> Bool
-
Removes a file syncrhonously.
Declaration
Swift
fileprivate func removeFile(atPath path: String)
-
Substracts from the cachesize the disk size passed as parameter. Logs an error message if the amount to be substracted is larger than the current used disk space.
Declaration
Swift
fileprivate func substract(diskSize: UInt64)
Parameters
diskSize
disksize to be deducted