@@ -11,50 +11,40 @@ import Foundation
11
11
public final class DefaultImageCache : ImageCacheProtocol {
12
12
13
13
// MARK: - Properties
14
- private let memoryCache = NSCache < NSString , NSData > ( )
14
+ private let memoryCache = MemoryCacheStorage ( )
15
+ private let diskCache = DiskCacheStorage ( )
15
16
16
- private let diskCache = DiskImageCache ( )
17
17
// 여기서 imageData 하나의 크기까지 지정해주려고 했는데 data 자체의 크기는 크지 않고, image로 바꾸는 연산이 들어가야하므로 빼기로 결정했습니다.
18
18
// private let imageCostLimit: Int
19
19
20
20
// 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)
26
23
}
27
24
28
25
// 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 )
40
37
}
41
- self . memoryCache. setObject ( fetchedData as NSData , forKey: key) // memoryCache에 저장
42
-
43
- completion ( fetchedData)
44
- } )
38
+ }
45
39
}
46
40
}
47
41
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)
58
48
}
59
49
}
60
50
0 commit comments