CacheIndex
open class CacheIndex
In-memory sorted index of cache entries.
Maintains entries sorted by modification date ascending (oldest first).
Provides O(log n) insertion via sort-after-append, O(1) lookup by filename,
and O(n) removal with index shift. All mutations update totalSize atomically.
This class is intentionally standalone so multiple DiskCache instances or
other consumers can share the same indexing logic without coupling to file I/O.
Usage
let index = CacheIndex()
// Build from filesystem
index.build(from: cacheURL)
// Track a new file
index.add(filename: "abc123", size: 4096)
// Mark a file as recently accessed (moves it to the end of the sorted order)
index.touch(filename: "abc123")
// Evict the oldest file
if let entry = index.popOldest() {
try? FileManager.default.removeItem(at: cacheURL.appendingPathComponent(entry.filename))
}
// Remove a specific file
index.remove(filename: "abc123")
-
A single entry in the cache index.
See moreDeclaration
Swift
public struct Entry : Equatable -
Total allocated disk size of all tracked entries, in bytes.
Declaration
Swift
open private(set) var totalSize: UInt64 { get } -
Number of entries in the index.
Declaration
Swift
open var count: Int { get } -
Whether the index contains no entries.
Declaration
Swift
open var isEmpty: Bool { get } -
The oldest entry (smallest
modificationDate), ornilif the index is empty.Declaration
Swift
open var oldest: Entry? { get } -
All entries sorted by modification date ascending.
Declaration
Swift
private var entries: [Entry] -
Mapping from filename to index in
entries.Declaration
Swift
private var entryMap: [String : Int] -
Serializes all mutations so
touchfrom any thread is safe with respect tocontrolCapacity()running oncacheQueue.Declaration
Swift
private let lock: NSRecursiveLock -
Declaration
Swift
public init() -
Builds the index by enumerating the filesystem directory.
Discards any existing index state and replaces it with the contents of the directory. Each file’s allocated size and modification date are read via the resource keys
isRegularFileKey,fileAllocatedSizeKey, andcontentModificationDateKey.Declaration
Swift
@discardableResult open func build(from directoryURL: URL) -> UInt64Parameters
directoryURLThe cache directory URL to enumerate.
Return Value
The total allocated size of all files found.
-
Inserts or replaces an entry in the index.
If an entry with the same
filenamealready exists, it is removed first. The entry’s modification date defaults to the current date whennil.Declaration
Swift
open func add(filename: String, size: UInt64, modificationDate: Date? = nil)Parameters
filenameMD5 filename of the cached file.
sizeAllocated disk size in bytes.
modificationDateFile modification date. Pass
nilto use the current date. -
Updates the modification date of an existing entry.
Does nothing if no entry with the given
filenameexists. The modification date defaults to the current date whennil.Declaration
Swift
open func touch(filename: String, modificationDate: Date? = nil)Parameters
filenameMD5 filename of the cached file.
modificationDateNew modification date. Pass
nilto use the current date. -
Removes an entry from the index by filename.
Does nothing and does not produce an error if the filename is not in the index.
Declaration
Swift
open func remove(filename: String)Parameters
filenameMD5 filename of the cached file to remove.
-
Removes all entries from the index and resets
totalSizeto zero.Declaration
Swift
open func removeAll() -
Removes and returns the oldest entry.
Declaration
Swift
open func popOldest() -> Entry?Return Value
The entry with the smallest
modificationDate, ornilif the index is empty. -
Rebuilds the filename→index mapping from the sorted entries array.
Declaration
Swift
private func rebuildEntryMap()
View on GitHub