Last active
April 20, 2019 10:49
-
-
Save SURYAKANTSHARMA/81917f58853318113c0b75d1447be4f6 to your computer and use it in GitHub Desktop.
Example of Unified Unit/Output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class OnDiskCache { | |
struct Item { | |
let path: String | |
let age: TimeInterval | |
let size: Int | |
} | |
var currentItems: Set<Item> { /* .returning current items.. */ } | |
func cleanCache(maxSize: Int) throws { | |
let sortedItems = self.currentItems.sorted { $0.age < $1.age } | |
var cumulativeSize = 0 | |
for item in sortedItems { | |
cumulativeSize += item.size | |
if cumulativeSize > maxSize { | |
try FileManager.default.removeItem(atPath: item.path) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment