forked from iina/iina
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCacheManager.swift
More file actions
64 lines (52 loc) · 2.01 KB
/
CacheManager.swift
File metadata and controls
64 lines (52 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//
// CacheManager.swift
// iina
//
// Created by lhc on 28/9/2017.
// Copyright © 2017 lhc. All rights reserved.
//
import Cocoa
class CacheManager {
static var shared = CacheManager()
var isJobRunning = false
var needsRefresh = true
private var cachedContents: [URL]?
private func cacheFolderContents() -> [URL]? {
if needsRefresh {
cachedContents = try? FileManager.default.contentsOfDirectory(at: Utility.thumbnailCacheURL,
includingPropertiesForKeys: [.fileSizeKey, .contentAccessDateKey],
options: [.skipsHiddenFiles, .skipsSubdirectoryDescendants])
}
return cachedContents
}
func getCacheSize() -> Int {
return cacheFolderContents()?.reduce(0 as Int) { totalSize, url in
let size = (try? url.resourceValues(forKeys: [.fileSizeKey]))?.fileSize ?? 0
return totalSize + size
} ?? 0
}
func clearOldCache() {
guard !isJobRunning else { return }
isJobRunning = true
let maxCacheSize = Preference.integer(for: .maxThumbnailPreviewCacheSize)
// if full, delete 50% of max cache
let cacheToDelete = maxCacheSize * FloatingPointByteCountFormatter.PrefixFactor.mi.rawValue / 2
// sort by access date
guard let contents = cacheFolderContents()?.sorted(by: { url1, url2 in
let date1 = (try? url1.resourceValues(forKeys: [.contentAccessDateKey]).contentAccessDate) ?? Date.distantPast
let date2 = (try? url2.resourceValues(forKeys: [.contentAccessDateKey]).contentAccessDate) ?? Date.distantPast
return date1.compare(date2) == .orderedAscending
}) else { return }
// delete old cache
var clearedCacheSize = 0
for url in contents {
let size = (try? url.resourceValues(forKeys: [.fileSizeKey]))?.fileSize ?? 0
if clearedCacheSize < cacheToDelete {
try? FileManager.default.removeItem(at: url)
clearedCacheSize += size
} else {
break
}
}
}
}