Created
April 20, 2019 11:39
-
-
Save SURYAKANTSHARMA/b6b4fd0e87e6cf6816b477c972480727 to your computer and use it in GitHub Desktop.
Input/Output refactoring
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
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