Skip to content

Instantly share code, notes, and snippets.

@SURYAKANTSHARMA
Created April 20, 2019 11:39
Show Gist options
  • Save SURYAKANTSHARMA/b6b4fd0e87e6cf6816b477c972480727 to your computer and use it in GitHub Desktop.
Save SURYAKANTSHARMA/b6b4fd0e87e6cf6816b477c972480727 to your computer and use it in GitHub Desktop.
Input/Output refactoring
protocol CleanUpPolicy {
func itemsToRemove(from items: Set<OnDiskCache.Item>) -> Set<OnDiskCache.Item>
}
struct MaxSizeCachePolicy: CleanUpPolicy {
let maxSize: Int
func itemsToRemove(from items: Set<OnDiskCache.Item>) -> Set<OnDiskCache.Item> {
var itemToRemove = Set<OnDiskCache.Item>()
let sortedItems = items.sorted { $0.age < $1.age }
var cumulativeSize = 0
for item in sortedItems {
cumulativeSize += item.size
if cumulativeSize > maxSize {
itemToRemove.insert(item)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment