Skip to content

Instantly share code, notes, and snippets.

@SURYAKANTSHARMA
Last active April 20, 2019 10:49
Show Gist options
  • Save SURYAKANTSHARMA/81917f58853318113c0b75d1447be4f6 to your computer and use it in GitHub Desktop.
Save SURYAKANTSHARMA/81917f58853318113c0b75d1447be4f6 to your computer and use it in GitHub Desktop.
Example of Unified Unit/Output
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