Created
April 20, 2019 11:39
-
-
Save SURYAKANTSHARMA/709fb04cf3516322caf06db0ac6d725d 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