Skip to content

Commit 04ca511

Browse files
committed
feat: ImageCacheProtocol 채택 및 fetch메소드 구현
1 parent 9466203 commit 04ca511

File tree

1 file changed

+22
-32
lines changed

1 file changed

+22
-32
lines changed

Queenfisher/Sources/ImageCache/ImageCacheImplements.swift

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,50 +11,40 @@ import Foundation
1111
public final class DefaultImageCache: ImageCacheProtocol {
1212

1313
// MARK: - Properties
14-
private let memoryCache = NSCache<NSString, NSData>()
14+
private let memoryCache = MemoryCacheStorage()
15+
private let diskCache = DiskCacheStorage()
1516

16-
private let diskCache = DiskImageCache()
1717
// 여기서 imageData 하나의 크기까지 지정해주려고 했는데 data 자체의 크기는 크지 않고, image로 바꾸는 연산이 들어가야하므로 빼기로 결정했습니다.
1818
// private let imageCostLimit: Int
1919

2020
// MARK: Initializers
21-
init(
22-
totalCostLimit: Int,
23-
countLimit: Int
24-
) {
25-
self.config(totalCostLimit: totalCostLimit, countLimit: countLimit)
21+
init(configType: ConfigType) {
22+
self.config(configType)
2623
}
2724

2825
// MARK: - Methods
29-
public func fetch(at url: URL, completion: @escaping (QFData?) -> Void) {
30-
let key = key(for: url)
31-
32-
if let data = memoryCache.object(forKey: key) {
33-
completion(data as QFData)
34-
}
35-
else {
36-
diskCache.fetch(at: url, completion: { [weak self] fetchedData in
37-
guard let self, let fetchedData else {
38-
completion(nil)
39-
return
26+
public func fetch(at url: URL, completion: @escaping (CacheableImage?) -> Void) {
27+
memoryCache.fetch(at: url) { [weak self] cacheableImage in
28+
if let cacheableImage {
29+
completion(cacheableImage)
30+
return
31+
}
32+
33+
// disk cache fetch
34+
self?.diskCache.fetch(at: url) { [weak self] diskImage in
35+
self?.executeDiskCacheLogic(diskImage: diskImage, url: url) { image in
36+
completion(image)
4037
}
41-
self.memoryCache.setObject(fetchedData as NSData, forKey: key) // memoryCache에 저장
42-
43-
completion(fetchedData)
44-
})
38+
}
4539
}
4640
}
4741

48-
func config(
49-
totalCostLimit: Int,
50-
countLimit: Int
51-
) {
52-
memoryCache.countLimit = countLimit
53-
memoryCache.totalCostLimit = totalCostLimit
54-
}
55-
56-
private func key(for url: URL) -> NSString {
57-
return url.absoluteString as NSString
42+
func config(_ configType: ConfigType) {
43+
memoryCache.config(
44+
countLimit: configType.memoryConfig.countLimit,
45+
totalCostLimit: configType.memoryConfig.totalCostLimit
46+
)
47+
diskCache.config(diskConfig: configType.diskConfig)
5848
}
5949
}
6050

0 commit comments

Comments
 (0)